简体   繁体   English

error NG6002:出现在AppModule的NgModule.imports中,但本身有错误

[英]error NG6002: Appears in the NgModule.imports of AppModule, but itself has errors

I am having angular 12 in my local and building project which runs fine without any error.我在本地和建筑项目中有 angular 12,它运行良好,没有任何错误。

My local setup:我的本地设置:

Angular CLI: 12.0.5  
Node: 12.16.3  
Package Manager: npm 6.14.4  
OS: win32 x64  
Angular: 12.0.5

But while building my project on the linux server it is giving this error with nothing much to work with.但是在 linux 服务器上构建我的项目时,它给出了这个错误,没有什么可以解决的。

Server setup:服务器设置:

Angular CLI: 12.0.5  
Node: 14.18.1  
Package Manager: npm 6.14.15  
OS: linux x64  
Angular: 12.0.5
Error: src/app/app-routing.module.ts:34:14 - error NG6002: Appears in the NgModule.imports of AppModule, but itself has errors  
34 export class AppRoutingModule { }

app.module.ts应用程序模块.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { HeaderComponent } from './header/header.component';
import { FooterComponent } from './footer/footer.component';
import { NotifyFormComponent } from './notify-form/notify-form.component';
import { LoginComponent } from './login/login.component';

@NgModule({
  declarations: [
    AppComponent,
    HomeComponent,
    HeaderComponent,
    FooterComponent,
    NotifyFormComponent,
    LoginComponent
  ],
  imports: [
    AppRoutingModule,
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app-routing.module.ts应用程序路由.module.ts

import { LoginComponent } from './login/login.component';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { NotifyFormComponent } from './notify-form/notify-form.component';
import { HomeComponent } from './home/home.component';

const routes: Routes = [
  {path:'',redirectTo:'/',pathMatch: 'full'},
  {
    path:'', 
    component: HomeComponent,
    children:[
      
    ]
  },
  {
    path:'notify', 
    component: NotifyFormComponent
  },
  {
    path:'login', 
    component: LoginComponent
  }
  // {
  //   path:'**', 
  //   component: HomeComponent
  // }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

tsconfig.json tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2017",
    "module": "es2020",
    "lib": [
      "es2018",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "enableI18nLegacyMessageIdFormat": false,
    "strictInjectionParameters": true,
    "strictInputAccessModifiers": true,
    "strictTemplates": true
  }
}

Please help me if I am doing anything horribly wrong here.如果我在这里做错了什么,请帮助我。 It is just a simple project to test with.这只是一个简单的测试项目。 Thanks a lot in advance.非常感谢。

I went on a bit of a research spree and here's an official issue for it https://github.com/angular/angular/issues/35399 .我进行了一些研究,这是一个官方问题https://github.com/angular/angular/issues/35399 Also here's another question for it error NG6002: Appears in the NgModule.imports of AppModule, but could not be resolved to an NgModule class .这里还有另一个问题, 错误 NG6002:出现在 AppModule 的 NgModule.imports 中,但无法解析为 NgModule 类 But I'll go ahead and summarize things I've found anyways.但我会继续总结我发现的东西。

  • For many people it worked simply restarting and rebuilding the project, or doing a full forced npm cache clear and reinstalling node_modules and rebuilding it.对于许多人来说,它只是简单地重新启动和重建项目,或者完全强制清除 npm 缓存并重新安装 node_modules 并重建它。

  • People have suggested disabling "ivy" in tsconfig works, however it's also been mentioned that while it works it's not the recommended way of fixing it.人们建议在 tsconfig 工作中禁用“ivy”,但是也有人提到,虽然它有效,但它不是推荐的修复方法。

  • This can apparently also happen if you've added components to imports instead of declarations.如果您将组件添加到导入而不是声明,这显然也会发生。

  • This can apparently also happen if you've installed some packages as root or if you for some reason don't have permissions on all the packages.如果您以 root 身份安装了一些软件包,或者由于某种原因您没有对所有软件包的权限,这显然也会发生。 Which was fixed with doing a chown on the directory, however the real solution is to install packages correctly in the first place and configuring the npm config so you don't ever install packages with root permissions.这是通过在目录上执行 chown 来修复的,但是真正的解决方案是首先正确安装软件包并配置 npm 配置,因此您永远不会安装具有 root 权限的软件包。 So essentially if you installed packages with root and try and run the project as non root, it will give this issue.因此,本质上,如果您使用 root 安装软件包并尝试以非 root 身份运行该项目,则会出现此问题。

  • Some people got the error when they added a service to imports instead of providers.有些人在向导入而不是提供者添加服务时遇到错误。

  • People also get this error if they name the package incorrectly ie SomeComponent instead of SomeComponentModule.如果人们错误地命名包,即 SomeComponent 而不是 SomeComponentModule,也会出现此错误。

  • In your specific case, you've imported the app router before browser module, I have no idea if that could cause an issue but everywhere I've seen the browser module is added before the app, so swap them around if nothing else work and see if that works.在您的特定情况下,您在浏览器模块之前导入了应用程序路由器,我不知道这是否会导致问题,但我所看到的所有地方都在应用程序之前添加了浏览器模块,因此如果没有其他工作,请交换它们,看看是否有效。

That's about everything I could find.这就是我能找到的一切。

您必须检查是否将所需的所有内容导入 app.module.ts,然后检查 linux 也许您需要添加任何其他组件,但这样应该可以正常工作

尝试 npm 版本应该 > 8.1.0 并且节点版本应该 > v16.13.0

I had this issue when I transferred a project from Windows to Linux.当我将项目从 Windows 转移到 Linux 时,我遇到了这个问题。
The paths on the imports for my modules had capital letters and this failed in linux and worked in windows.我的模块的导入路径有大写字母,这在 linux 中失败并在 Windows 中工作。
For example either of the following will work on Windows:例如,以下任一项都可以在 Windows 上运行:

import { switchMap, tap } from 'rxjs/operators';
import { switchMap, tap } from 'rxjs/Operators';

On Linux only the lower case version will work:在 Linux 上,只有小写版本可以工作:

import { switchMap, tap } from 'rxjs/operators';

我用ng serve命令解决了这个问题。当您更改 angular.json 文件中的配置时,您会遇到这个问题。

check if the paths mentioned in 'templateUrl' and 'styleUrls' are correct in the components wich are related to this module who has a problem,检查'templateUrl'和'styleUrls'中提到的路径在与有问题的模块相关的组件中是否正确,

@Component({
    selector: 'app-navigation-menu-item',
    templateUrl: './xxx.component.html',
    styleUrls: ['./xxx.component.scss']
})

in my case, this was the bug.就我而言,这是错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 出现在ExtendedSearchModule的NgModule.imports,但本身有错误? - Appears in the NgModule.imports of ExtendedSearchModule, but itself has errors? AppModule中的ERROR不是NgModule,typescript 2.3.2,Angular 4 - ERROR in AppModule is not an NgModule, typescript 2.3.2, Angular 4 编译器失败 - 找不到“AppModule”的 NgModule 元数据中的错误 - Compiler fails - ERROR in No NgModule metadata found for 'AppModule' 找不到“ AppModule”的NgModule元数据中的错误。 和webpack:编译失败 - ERROR in No NgModule metadata found for 'AppModule'. and webpack: Failed to compile 未捕获的错误:模块'AppModule'导入了意外的指令'ProductsComponent'。 请添加@NgModule批注 - Uncaught Error: Unexpected directive 'ProductsComponent' imported by the module 'AppModule'. Please add a @NgModule annotation 将 AngularJS 升级到 Angular,AppModule 被引导,但它没有声明“@NgModule.bootstrap”错误 - Upgrading AngularJS to Angular, AppModule was bootstrapped, but it does not declare "@NgModule.bootstrap" Error NG002 错误:无法解析为 NgModule 类 - NG002 Error: Could not be resolved to an NgModule class 由模块“AppModule”导入的意外值“FormBuilder”。 请添加@NgModule 注释 - Unexpected value 'FormBuilder' imported by the module 'AppModule'. Please add a @NgModule annotation 重新安装node_modules后找不到“ AppModule”的NgModule元数据 - No NgModule metadata found for 'AppModule' after reinstalling node_modules 网站出现脚本错误,显示为空白 - Website has script errors, appears blank
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM