错误 NG6002:出现在 AppModule 的 NgModule.imports 中,但无法解析为 NgModule 类

Pab*_*uza 164 angularfire angular google-cloud-firestore

第一次使用 firestore 时出现此错误。根据我的研究,这似乎是 Ivy 的问题。我没有很多修改 tsconfig.app.json 的经验,这是我被指出的方向,遵循其他答案。

我能够从原始项目中修改的唯一一件事是使用 Angular Fire 6 而不是 5,这是我最初按照教程所做的。

这是 package.json:

{
  "name": "language",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~9.0.1",
    "@angular/cdk": "^9.0.0",
    "@angular/common": "~9.0.1",
    "@angular/compiler": "~9.0.1",
    "@angular/core": "~9.0.1",
    "@angular/fire": "^6.0.0-rc.1",
    "@angular/flex-layout": "^9.0.0-beta.29",
    "@angular/forms": "~9.0.1",
    "@angular/material": "^9.0.0",
    "@angular/platform-browser": "~9.0.1",
    "@angular/platform-browser-dynamic": "~9.0.1",
    "@angular/router": "~9.0.1",
    "firebase": "^7.8.2",
    "rxjs": "~6.5.4",
    "rxjs-compat": "^6.5.4",
    "tslib": "^1.10.0",
    "zone.js": "~0.10.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.900.2",
    "@angular/cli": "~9.0.2",
    "@angular/compiler-cli": "~9.0.1",
    "@angular/language-service": "~9.0.1",
    "@types/node": "^12.11.1",
    "@types/jasmine": "~3.3.8",
    "@types/jasminewd2": "~2.0.3",
    "codelyzer": "^5.1.2",
    "jasmine-core": "~3.4.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.1.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "^1.4.0",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.15.0",
    "typescript": "~3.7.5",
    "@angular-devkit/architect": "^0.900.0-0 || ^0.900.0",
    "firebase-tools": "^7.12.1",
    "fuzzy": "^0.1.3",
    "inquirer": "^6.2.2",
    "inquirer-autocomplete-prompt": "^1.0.1"
  }
}
Run Code Online (Sandbox Code Playgroud)

angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "language": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/language",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "aot": true,
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "./node_modules/@angular/material/prebuilt-themes/deeppurple-amber.css",
              "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,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "10kb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "language:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "language:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "language:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "./node_modules/@angular/material/prebuilt-themes/deeppurple-amber.css",
              "src/styles.scss"
            ],
            "scripts": []
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "tsconfig.app.json",
              "tsconfig.spec.json",
              "e2e/tsconfig.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        },
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "language:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "language:serve:production"
            }
          }
        },
        "deploy": {
          "builder": "@angular/fire:deploy",
          "options": {}
        }
      }
    }
  },
  "defaultProject": "language"
}
Run Code Online (Sandbox Code Playgroud)

tsconfig.app.json

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./out-tsc/app",
    "types": [],
  
  },
  "files": [
    "src/main.ts",
    "src/polyfills.ts"
  ],
  "include": [
    "src/**/*.d.ts"
  ],
  "exclude": [
    "src/test.ts",
    "src/**/*.spec.ts"
  ]
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

小智 205

您的模块尚未由 Angular 服务器加载node ng serve,因此请重新启动您的服务器,以便服务器加载您刚刚添加的模块@NgModule app.module.ts

  • 您需要重新启动 VSCode 才能让 Angular Language Service 重新编译包。 (9认同)
  • 我发现很多时候重新启动服务器或重新启动 IDE 就能解决问题:D (4认同)
  • 谢谢你!简单但对我有用。VS Code 重新加载:F1 > 输入重新加载窗口 > 按 Return 再次感谢。 (3认同)
  • 实际上 angular 应该在错误日志本身中提供一个提示来重新启动和检查! (2认同)

小智 104

当您在imports: []而不是中添加组件声明时会显示此错误declarations: [],例如:

declarations: [
  AppComponent,
],
imports: [
  BrowserModule,
  AppRoutingModule,
  SomeComponent <-----------wrong
],
Run Code Online (Sandbox Code Playgroud)

  • 我不同意——这不是我(或许多其他人)的答案。 (15认同)
  • 这对我来说是正确的答案,但我必须重新启动 (2认同)

mat*_*the 45

当我犯了导入MatSnackBar而不是MatSnackBarModulein的愚蠢错误时,我收到了这个错误app.module.ts

  • 老白骨在这里 - MatCheckboxModule - 你的回答很有用,谢谢 (6认同)
  • 我多年来第一次登录只是为了表示感谢;) (2认同)

Was*_*siF 20

我通过serviceimports数组中添加一个而不是数组而犯了一个错误providers

@NgModule({
  imports: [
    MyService // wrong here
  ],
  providers: [
    MyService // should add here
  ]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

Angular说你需要添加Injectablesproviders数组中。


Pab*_*uza 15

通过根据文档选择退出常春藤来修复它。

https://angular.io/guide/ivy

更改 tsconfig.app.json 以选择退出 Ivy。

"enableIvy": false
Run Code Online (Sandbox Code Playgroud)

  • 各位,禁用 Ivy 并不是正确的方法,它很快将成为 Angular 唯一的渲染引擎。我理解如果您由于某种原因需要尽快推送到“prod”,但您应该诊断并找出真正导致问题的原因。 (52认同)
  • @AaronJordan 我认为问题更在于 Ivy 还没有做好生产准备,这与 Angular 团队的说法相反。生产就绪的升级不应该有“接近”Ivy 的错误数量。此外,出于向后兼容性的原因,View Engine 不会很快消失(这与 Angular 库构建仍然发生在 View Engine 中而不是 Ivy 中的原因相同) (4认同)

小智 12

这对我有用:

1)停止ng服务器

2)重新安装你的包

npm install your-package-name
Run Code Online (Sandbox Code Playgroud)

3)再次运行

ng serve
Run Code Online (Sandbox Code Playgroud)


小智 12

有时,当您在项目中更改/更新@NgModule时,项目会被编译但更改不会得到反映。所以,如果你没有得到它,重新启动一次以获得预期的结果。 服务


小智 8

尝试清除缓存

  1. npm cache clean --force
  2. npm install

  • 似乎正确的命令是“npm cache clean --force” (3认同)

mar*_*sze 8

我的错误:我错过了Module我写的部分

import { MatPaginator } from '@angular/material/paginator';
Run Code Online (Sandbox Code Playgroud)

代替

import { MatPaginatorModule } from '@angular/material/paginator';
Run Code Online (Sandbox Code Playgroud)

在我的模块中。


Sha*_*uti 5

对我来说,我在 Ubuntu 下工作

如果我将sudong 一起使用,错误就会消失

sudo ng build
sudo ng serve
Run Code Online (Sandbox Code Playgroud)

更好的解决方案是使用以下命令授予当前用户对当前文件夹所需的权限:

sudo chown -R `whoami` ./
Run Code Online (Sandbox Code Playgroud)

这样做将使您无需 sudo 即可运行 ng 命令。


XRa*_*cat 5

当使用错误的导入(例如使用自动导入时)时,也会发生这种情况。我们以 MatTimePickerModule 为例。这将给出类似于问题中描述的错误消息:

import { NgxMatTimepickerModule } from '@angular-material-components/datetime-picker/lib/timepicker.module';
Run Code Online (Sandbox Code Playgroud)

这应该是

import { NgxMatTimepickerModule } from '@angular-material-components/datetime-picker';
Run Code Online (Sandbox Code Playgroud)

  • 只是我的一分钱。该文件的导入为“import { MatFormField, MatHint, MatLabel } from '@angular/material/form-field';”。它应该是 `import { MatFormFieldModule } from '@angular/material/form-field';` **当导入错误时,也会发生相同的错误**。我花了1小时。我差点就要禁用 Ivy。**我使用的是 Angular 版本 11** (2认同)

use*_*932 5

在我来说,我通过设置“strictTemplates”回到这个解决tsconfig.json

{
  "compilerOptions": {
    // ...
  },
  "angularCompilerOptions": {
    "strictTemplates": false
  }
}
Run Code Online (Sandbox Code Playgroud)

我之前已将其设置为true以防止 VStudio 弹出窗口显示:

在此处输入图片说明