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

24

97

I get the error in the subject of this post when I use the CLI to create a new component: ng g c my-component --project=my-project (No component is created)

I've seen other posts with the same error message, but none of them also include Nrwl/Nx, which I believe is somehow involved. Project started as Ng4 and was upgraded to Ng6.

Environment:

  • Angular CLI: 6.0.8
  • Node: 8.9.4
  • OS: win32 x64
  • Angular: 6.0.6
  • @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/cdk 6.3.1
  • @angular/cli 6.0.8
  • @angular/material 6.3.1
  • @ngtools/webpack 6.0.8
  • @schematics/angular 0.6.1
  • @schematics/update 0.6.8
  • rxjs 6.2.1
  • typescript 2.7.2
  • webpack 4.8.3
  • nrwl/nx 6.1.0

UPDATE #1

I tried dropping the --project flag entirely ( so just ng g c my-component) and still get the same problem. Not sure what that means, yet.

Chartres answered 5/7, 2018 at 22:57 Comment(1)
Please try this approach https://mcmap.net/q/218879/-angular2-could-not-find-an-ngmodule Should be ok.Striate
C
2

I ended up totally deleting node_modules, clearing out the yarn cache and then running yarn to reinstall all packages. I also edited my angular.json file so that my pathing looked like this for all applications and libraries:

"root": "libs/my-lib",
"sourceRoot": "libs/my-lib/src"

I'm not sure which of those fixed the problem, but all appears to be OK now. At least the immediate problem went away.

Chartres answered 9/7, 2018 at 15:11 Comment(0)
P
166

Quick fix

1) Change current directory in cmd/terminal to src/app

cd src/app (linux)

cd .\src\app\ (windows)

2) Run commands now

ng g c myComponent

Priam answered 12/7, 2018 at 4:47 Comment(2)
The reason why this works, is that your project's default src/app folder hierarchy has been changed. Therefore, anything else that doesn't fit this situation will not work. When you generate a default project with cli, you shouldn't have to do this.Harmsworth
That is not the only reason, if you run the ng generate component in a folder different to src/app you will get that error.Effluence
I
39

The generate of components aims to do 2 things:

  • create source code for the component
  • registers the component in a module (by default, in the app.module.ts)

The problem is, that you don't have an app.module.ts. You renamed it to something else, or perhaps even have multiple of them. In that case there are 2 possible solutions:

  1. you do the registration of the component manually. (easiest)

    ng g component mycomponent --skip-import

  2. you specify the module with the -m switch. (might be broken) There is a long thread about it here: https://github.com/nrwl/nx/issues/526

Isolated answered 18/3, 2019 at 17:16 Comment(2)
Or you moved app.module.ts to another folder such as modules folderObelisk
or you are not in src/app folder so app.modules.ts is not available in the current. see the reply from @PriamQuintilla
P
23

Run component generation command(ng g c component-name) from where the app.module.ts file is located.

Puffy answered 11/10, 2019 at 18:11 Comment(0)
T
16

You are not inside app folder... cd app on windows, and then run command

Tremayne answered 9/12, 2020 at 11:31 Comment(0)
P
9

Working 100%

Try --skip-import

ng g c mycomponent --skip-import

or change directory src/app ;

open the terminal on click on app -> enter comand ng g c 'your component name' and hit enter

I think These are the error you getting click once and check it https://i.stack.imgur.com/RSG7c.png

Palm answered 3/10, 2020 at 15:41 Comment(0)
L
8

Your current directory in cmd window matters. Run this command from the 'src/app' folder in the project. It should work.

Lingwood answered 12/4, 2020 at 13:57 Comment(0)
F
3

I removed the line "cli": { "defaultCollection": "@nativescript/schematics" } from angular.json file and it worked for me.

Fried answered 25/7, 2019 at 10:20 Comment(0)
A
3

You may have changed the file app.module.ts to other folder (out of folder app). Ionic is not finding the file.

Axon answered 22/9, 2020 at 13:47 Comment(0)
S
3

Please check the location for .module.ts file. I was running this command from /src folder, while app module was inside /src/app folder.

When run the same command from /src/app path. It is working.

Sensory answered 17/9, 2021 at 6:31 Comment(0)
C
2

I ended up totally deleting node_modules, clearing out the yarn cache and then running yarn to reinstall all packages. I also edited my angular.json file so that my pathing looked like this for all applications and libraries:

"root": "libs/my-lib",
"sourceRoot": "libs/my-lib/src"

I'm not sure which of those fixed the problem, but all appears to be OK now. At least the immediate problem went away.

Chartres answered 9/7, 2018 at 15:11 Comment(0)
W
2

I had the same issue, In angular.json file, I searched for my-project-e2e (replace my-project with your project name) and then I changed:

"root": "" to "root": "e2e" and saved it. Everything was working again.

The solution was specified in this link.

Wyne answered 8/8, 2018 at 16:25 Comment(0)
H
2
  1. First way:
    I think first you have to go to inside your app folder then type cmd;
    ng g c your_com_name --spec false
    or ng g c your_com_name
    It will definitely create your component or solve your problem.

  2. second way:
    Remove your e2e from your angular.json->projects:
    definitely

    { 
        your_project_name-e2e:{}
    }
    

    example: my-first-app-e2e

    {
          "root": "e2e/",
          "projectType": "application",
          "architect": {
            "e2e": {
              "builder": "@angular-devkit/build-angular:protractor",
              "options": {
                "protractorConfig": "e2e/protractor.conf.js",
                "devServerTarget": "my-first-app:serve"
              }
            },
            "lint": {
              "builder": "@angular-devkit/build-angular:tslint",
              "options": {
                "tsConfig": "e2e/tsconfig.e2e.json",
                "exclude": [
                  "**/node_modules/**"
                ]
              }
            }
          }
        }
    
Hildehildebrand answered 10/8, 2019 at 19:31 Comment(0)
T
2

This is pretty weird answer, but the problem occur because I have open my project in the src directory only. Reopening the entire project folder resolves the issue.

Trying answered 1/5, 2020 at 2:5 Comment(1)
I think this is the simplest answer to this issue. The users should be in the root folder while performing Angular CLI commands.Suribachi
S
2

Make sure you are in the right path (I mean in the path you are working in) so just verify this in the console by print working directory (pwd) and change it. This worked for me

Smearcase answered 31/5, 2021 at 19:58 Comment(0)
E
2

Enter ** cd .\app\ ** in the terminal or type "cd app + Tab". Once you are in the app folder, the ng command works!

Eppie answered 27/1, 2022 at 11:4 Comment(0)
E
1

This also happened to me and in my case it was due to me generating a new component outside of the app folder, I bought the new component inside the app folder and the issue was solved.

Eugenides answered 26/7, 2019 at 11:30 Comment(0)
P
1

Hopefully helps someone, I had 2 issues:

  1. I had 2 modules on the same folder level as app.module.ts that referenced NgModule, I moved it to a separate folder

  2. My components folder was outside of the app folder, you have to be cded into app in order to run the command for it to find NgModule

Pastime answered 26/4, 2023 at 23:24 Comment(0)
B
0

Had same issue. Turned out to be somehow related to the name of the root component. Renaming back to 'app' resolved the problem

Bedwarmer answered 5/11, 2018 at 4:21 Comment(0)
V
0

I had the same issue. My problem (in ionic 4) was that the file app-routing.module.ts was missing. I had to create an empty one, before adding new pages worked:

import { NgModule } from '@angular/core';
import { PreloadAllModules, RouterModule, Routes } from '@angular/router';

const routes: Routes = [
    {
        path: '',
        redirectTo: 'home',
        pathMatch: 'full'
    },
];

@NgModule({
    imports: [
        RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })
    ],
    exports: [RouterModule]
})
export class AppRoutingModule { }
Visional answered 15/7, 2020 at 9:16 Comment(0)
M
0

From visual studio code I made a: right click on the app folder -> click on "open in integrated terminal -> run the cmd: ng g component user. 4 files have been created. It works successfully!

Melanochroi answered 7/7, 2021 at 3:34 Comment(0)
C
0

I created empty folder my own and move my old module into it. After when I tried to create component inside the folder I got same error. I deleted the empty folder and create module folder. After that I was able to create new components etc.

Caparison answered 31/8, 2022 at 8:4 Comment(0)
T
0

I got this same error because I was in the wrong file location. For some reason I thought I had to do it from the file location src instead of the location above (where angular.json is and ofcourse src/ itself)

Doing the command from root location solved it (in my case cd ..).

Tektite answered 8/11, 2022 at 19:35 Comment(0)
Q
0

you have to move to the src/app folder

simple use $cd app command to go inside

Quinn answered 27/3, 2023 at 16:47 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Staten
R
0

This worked for me with angular 16.2.8. Here is my package.json showing my angular version dependencies: enter image description here

In this example answer I have not converted my angular application to use standalone components yet. If you use --skip-import then imports will not be added to your app.module.ts which will lead to your application having problems. In order to make sure imports are added to app.module.ts, when creating a component I had to cd to my directory where my app.module.ts was located. In my case that was the folder:

(base-folder)/src/app enter image description here

After being in my folder where my app.module.ts was located I typed the command

ng g c NewComponentName -m app

If you have changed your app.module.ts to a new name then replace app in the command above with that name.

For a real example in my example below I created a component called PrivacyPolicy enter image description here

You can see my app.module.ts added the imports successfully from the blue UPDATE text.

Here is what the import looks like in my app.module.ts after adding the component. Notice I moved my component to a components folder manually after creating my component which is why my path starts with components: enter image description here

Rambunctious answered 6/1, 2024 at 20:15 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.