Search code examples
angulartypescriptangular-cli

Angular CLI generate component issue


When generating a new component using ng g c my-component I get the error:

Could not find an NgModule. Use the skip-import option to skip importing in NgModule.

If I then use the skip-import option Angular generates the component in my e2e folder.

I'm assuming it's something to do with my angular.json file which used to be called angular-cli.json prior to me running ng upgrade which is no doubt what caused the issue as there seems to have been some structural changes.

Is there way to fix this without creating a new project with the CLI and then manually copying over files..?

ng g c ran from root "my-app" folder.

Folder structure:

my-app
 - dist
 - e2e
 - node_modules
 - src
   - app
   - etc

angular.json within my-app folder

**Angular Versions:**
@angular-devkit/architect         0.6.8
@angular-devkit/build-angular     0.6.8
@angular-devkit/build-optimizer   0.6.8
@angular-devkit/core              0.6.8
@angular-devkit/schematics        0.6.8
@angular/cli                      6.0.8
@ngtools/webpack                  6.0.8
@schematics/angular               0.6.8
@schematics/update                0.6.8
rxjs                              5.5.2
typescript                        2.5.3
webpack                           4.8.3

Solution

  • Answer/update for anyone having the same issue.

    You can cd to the component directory and ng g c will work fine.

    To be able to run this from the root folder I rolled back my version of @angular/cli to version 6.0.3. I had 6.0.8 installed previously.

    npm i @angular/[email protected] to install older version

    ng g c your-component will then work from the root folder as normal.