Angular-cli: "Could not find an NgModule" when trying to generate a component and accessing the project directory trough a symlink

Created on 7 May 2018  路  12Comments  路  Source: angular/angular-cli

When your project directory is in a another directory, accessed through a symlink, ng generate component some-component can't find any NgModule.

Versions

Angular CLI: 6.0.0
Node: 8.11.1
OS: linux 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

Developing on Bash on Ubuntu on Windows 10 1803.

Repro steps

  • Create a symlink to your dev directory (in my case /home/myuser/dev-> /mnt/d/dev)
  • Access your dev directory through the symlink
  • Create a new Angular project with ng new myNewProject
  • Get inside the newly created project directory with cd myNewPoject
  • Try to generate a new component with ng generate component some-component

Observed behavior

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

Desired behavior

It should generate a new component as if you were not following the symlink.

Most helpful comment

angular.json -->

projects: {

project_name-e2e:{} <-- delete

}

All 12 comments

Closing...
Duplicate of #10653

angular.json -->

projects: {

project_name-e2e:{} <-- delete

}

You must target the default project name like this:
ng generate component components/test --project {{projectName}}

@tianxiaozi Your solution worked ! Thanks a lot !!

I had accidentally deleted my app.module.ts file. Restoring it helped in fixing the issue.

@NikolayDonev
your solution worked for me thank you very much

Check if your app.module.ts lives in the app folder.

I moved my app.module.ts file in a new folder named "modules". That's why I got that error. I moved back it into app folder and the issue is solved.

Try creating the component inside the "app" module which is inside src folder. It worked for me.

I found that you need to have a module file, with
import { NgModule } from '@angular/core';
In the path when you use a command like
ng generate module client-search

You can only use ng g c "component name" in the same folder as the file: "app-routing.module" because of the import that typescript is doing

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

_This action has been performed automatically by a bot._

Was this page helpful?
0 / 5 - 0 ratings