Could not find an NgModule. Use the skip-import op

2020-06-07 05:15发布

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.

Any ideas how I can fix this?

Thx.

TTE

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.

标签: angular nrwl
11条回答
forever°为你锁心
2楼-- · 2020-06-07 06:03

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.

查看更多
forever°为你锁心
3楼-- · 2020-06-07 06:04

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

It should work.

查看更多
Emotional °昔
4楼-- · 2020-06-07 06:05

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

查看更多
Luminary・发光体
5楼-- · 2020-06-07 06:08

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.

查看更多
成全新的幸福
6楼-- · 2020-06-07 06:09

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

查看更多
登录 后发表回答