ng generate error
Versions
Angular CLI: 6.0.0 Node: 8.9.1 OS: linux x64 Angular: 6.0.0 ... animations, cdk, cli, common, compiler, compiler-cli, core ... forms, http, language-service, material, platform-browser ... platform-browser-dynamic, platform-server, router
Package Version
angular-devkit/architect 0.6.0 angular-devkit/build-angular 0.6.0 angular-devkit/build-optimizer 0.6.0 angular-devkit/core 0.6.0 angular-devkit/schematics 0.6.0 ngtools/webpack 6.0.0 schematics/angular 0.6.0 schematics/update 0.6.0 rxjs 6.1.0 typescript 2.7.2 webpack 4.6.0
Repro steps
ng g component home
Could not find an NgModule. Use the skip-import option to skip importing in NgModule.
ng g component home --skip-import (same as generate everything)
Success generate component but wrong path not inside "src" but create new folder in root "home/user/PATH/app-id/home/home.component.*

you are correct, I have correctly fixed my issue following your directions. thank you!
On May 6, 2018, at 6:31 PM, San Nguyen [email protected] wrote:
@johren01 https://github.com/johren01 I mean with a newly created project, cli will generate it under "src" folder, but if we generate a new app, by ng g app, generated files will be placed under "projects" folder. So I think when using ng g c home --project projName, if the projName is not placed under "projects" folder, the generated path might be wrong. I have tried but mine works fine for both project under "src" folder and "projects" folder. So I think may be your global cli version was not upgraded to 6. That what I guess based on your provided information. I can't reproduce your issue now on my macOS machine
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/angular/angular-cli/issues/10653#issuecomment-386921837, or mute the thread https://github.com/notifications/unsubscribe-auth/ARtB_VmdanzzhnuL1FWMVSgd2EylZE_Aks5tv3m5gaJpZM4TzTZc.
Haha my issue was much more of a noob mistake. Upgraded my ng cli globally, but did not exit terminal for it to refresh (my guess) because now it seems to work just fine. :)
I've solved my issued on my computer. My problems are below:
- Because my working path is too deep, so I used command line
$ ln -sto create the link on my root path. - I go to my project use the link which I created. When I use the command line
$ pwdto get the path that is/Users/admin/angular/newProject. But my original path is/Users/admin/Desktop/localDisk/04-www/angular/newProject.
Solve the problem
- Go to the project through original path
The angular results are below:
Mac:day01 admin$ ng g c components/header
CREATE src/app/components/header/header.component.css (0 bytes)
CREATE src/app/components/header/header.component.html (25 bytes)
CREATE src/app/components/header/header.component.spec.ts (628 bytes)
CREATE src/app/components/header/header.component.ts (269 bytes)
UPDATE src/app/app.module.ts (481 bytes)
Here is my angular info
Angular CLI: 6.0.0
Node: 9.4.0
OS: darwin x64
Angular: 6.0.0
... animations, cli, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.6.0
@angular-devkit/build-angular 0.6.0
@angular-devkit/build-optimizer 0.6.0
@angular-devkit/core 0.6.0
@angular-devkit/schematics 0.6.0
@ngtools/webpack 6.0.0
@schematics/angular 0.6.0
@schematics/update 0.6.0
rxjs 6.1.0
typescript 2.7.2
webpack 4.6.0
@WillsXiao, It shoudn’t be so difficult do generate things, that may be a workaround, not a solution.
@sandangel Problem solved, my case is same as @WillsXiao
@nginx17 please reopen the issue, this is not a solution, just a workaround, you’re not supoosed to create a link for every new project, it should work out of the box.
@nginx17 please reopen the issue. This is a work around and not a solution.
I am getting two different problems when I try to use ng generate.
From the app folder root if I run ng g c my-component I get an error Could not find an NgModule. Use the skip-import option to skip importing in NgModule.
If I try to generate a service ng g service data It builds a User directory starting from the root of my machine inside of my app with the data.service.ts file inside that.
This is quite comical. I guess something was missed with the version 6.0.0 release. The errors are not even consistent. One command does a ridiculous thing and one throws an error and says it can't find a module when clearly there is one present.
I have restarted my terminal which seemed like a likely solution. I ran npm cache verify after I uninstalled the old angular cli. I updated my machine and restarted it. I also tried uninstalling angular cli and installed it again. This did not fix it.
Has anyone been able to use ng generate properly the way it used to work with @angular/cli": "~1.7.1"?
@WillsXiao I don't understand your workaround. How do you find the original path?
@johren01
My original path
/Users/admin/Desktop/test/demo/aaa/bbb/ccc/ngProject
Create a link for original path
$ ln -s /Users/admin/Desktop/test/demo/aaa/bbb/ccc/ngProject /Users/admin/ngProject
Go to the ngProject through link
$ cd /Users/admin/ngProject
Now my ngProject path is /Users/admin/ngProject. It is different to the original path. If I go to the ngProject through the link, that the $ ng g c myComponent couldn't be working. If I go to ngProject through the original path, that the $ ng g c myComponent could be working.
My manual solution for generating a service on mac:
-
From project directory root run
cd src/app -
From app directory root run
touch some.service.ts -
The only big difference is you will not need to inject the service into
app.mpdule.ts. see example below.
//version 6.0.0
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class SomeService {
constructor() { }
}
//version 5.2.0
import { Injectable } from '@angular/core';
@Injectable()
export class SomeService {
constructor() { }
}
There is no need to import the service into app.module with angular version 6.0.0 and no need to add it to providers: as well.
If you are updating an app that is built using the angular version 5.2.0 and angular cli version 1.7.1. You can just move the component directories over and inject them into the app.module.ts like before. The only snag I had moving my components over was updating the path to rxjs modules.
Pipes can also be carried over from 5.2.0 and injected the same way.
I know this is not at all a fix for this problem in any way, but when you have to work around it's really not that hard to do it yourself. I just wanted to point out the BIG diff with services. It's totally worth switching over because it is so much faster!
Of course, if there is a real fix for making ng generate stuff using the correct path to app root, please let me know about it!
Does anyone have a repository that reproduces this issue? I am working to reproduce this locally, but that could help move things along.
Thanks in advance!
Hey @Brocco, this happens for me even by creating a new app:
ng new angular --routing --style=scss
then
ng g c test-component or ng g module test-module
@matheusdavidson I have tried that without luck... can you post the content of both your project's angular.json and if you have one the contents of .angular-cli.json in your user directory?
And if you happen to have any other "angular" files in your home dir (.angular.json, angular.json, .angular-config.json) the content of those would be super helpful too!
Will generate a new app and give you those details, one moment
@Brocco I just did ng new for this app. ng g c mycomponent still throwing error.
https://github.com/iposton/testing-angular6
ng --version
Angular CLI: 6.0.0
Node: 8.11.1
OS: darwin x64
Angular: 6.0.0
... animations, cli, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.6.0
@angular-devkit/build-angular 0.6.0
@angular-devkit/build-optimizer 0.6.0
@angular-devkit/core 0.6.0
@angular-devkit/schematics 0.6.0
@ngtools/webpack 6.0.0
@schematics/angular 0.6.0
@schematics/update 0.6.0
rxjs 6.1.0
typescript 2.7.2
webpack 4.6.0
angular.json below
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"testing": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/testing",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.css"
],
"scripts": []
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "testing:build"
},
"configurations": {
"production": {
"browserTarget": "testing:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "testing:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.spec.json",
"karmaConfig": "src/karma.conf.js",
"styles": [
"styles.css"
],
"scripts": [],
"assets": [
"src/favicon.ico",
"src/assets"
]
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"src/tsconfig.app.json",
"src/tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
}
},
"testing-e2e": {
"root": "e2e/",
"projectType": "application",
"architect": {
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "testing:serve"
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": "e2e/tsconfig.e2e.json",
"exclude": [
"**/node_modules/**"
]
}
}
}
}
},
"defaultProject": "testing"
}
ng --version
Angular CLI: 6.0.0
Node: 9.10.1
OS: darwin x64
Angular: 6.0.0
... animations, cli, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.6.0
@angular-devkit/build-angular 0.6.0
@angular-devkit/build-optimizer 0.6.0
@angular-devkit/core 0.6.0
@angular-devkit/schematics 0.6.0
@ngtools/webpack 6.0.0
@schematics/angular 0.6.0
@schematics/update 0.6.0
rxjs 6.1.0
typescript 2.7.2
webpack 4.6.0
Generate the app / current directory
ng new project --routing --style=scss
pwd: /var/www/project
Generate command
ng g c test-component

angular.json
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"project": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {
"@schematics/angular:component": {
"styleext": "scss"
}
},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/project",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.scss"
],
"scripts": []
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "project:build"
},
"configurations": {
"production": {
"browserTarget": "project:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "project:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.spec.json",
"karmaConfig": "src/karma.conf.js",
"styles": [
"styles.scss"
],
"scripts": [],
"assets": [
"src/favicon.ico",
"src/assets"
]
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"src/tsconfig.app.json",
"src/tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
}
},
"project-e2e": {
"root": "e2e/",
"projectType": "application",
"architect": {
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "project:serve"
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": "e2e/tsconfig.e2e.json",
"exclude": [
"**/node_modules/**"
]
}
}
}
}
},
"defaultProject": "project"
}
Folder directory screenshot

@Brocco, if you need anything else i'm here.
@Brocco, here is the repo of the project i've just created, i generated a module just to show you whats happening: https://github.com/matheusdavidson/project
Uninstalling angular/cli and reinstalling seems to have solved this problem for me ...