[angular] Angular CLI - Please add a @NgModule annotation when using latest

note: I'm new to Angular, so please excuse any new comer stupidity here.

Details

  • I've installed the latest version of Angular CLI
  • The default app loads and runs perfectly fine after 'ng serve'

Issue

  • I decided to create a new module an import it into the app module
  • This is something I've done a couple of times in Angular 2 and it's worked perfectly fine
  • However, since I ran the latest version of Angular CLI this morning, importing a module breaks and I receive the following error:

compiler.es5.js:1689 Uncaught Error: Unexpected directive 'ProjectsListComponent' imported by the module 'ProjectsModule'. Please add a @NgModule annotation.

App Module

_x000D_
_x000D_
import { BrowserModule } from '@angular/platform-browser';_x000D_
import { NgModule } from '@angular/core';_x000D_
import { AppComponent } from './app.component';_x000D_
import { AngularFireModule } from 'angularfire2';_x000D_
import { AngularFireDatabaseModule } from 'angularfire2/database';_x000D_
import { AngularFireAuthModule } from 'angularfire2/auth';_x000D_
import { environment } from '../environments/environment';_x000D_
import { ProjectsModule } from './projects/projects.module';_x000D_
import { HttpModule } from '@angular/http';_x000D_
_x000D_
_x000D_
@NgModule({_x000D_
  imports: [_x000D_
    BrowserModule,_x000D_
    HttpModule,_x000D_
    ProjectsModule,_x000D_
    AngularFireModule.initializeApp(environment.firebase, 'AngularFireTestOne'), // imports firebase/app needed for everything_x000D_
    AngularFireDatabaseModule, // imports firebase/database, only needed for database features_x000D_
    AngularFireAuthModule // imports firebase/auth, only needed for auth features_x000D_
  ],_x000D_
_x000D_
  declarations: [AppComponent],_x000D_
_x000D_
  bootstrap: [AppComponent]_x000D_
_x000D_
})_x000D_
_x000D_
export class AppModule { }
_x000D_
_x000D_
_x000D_

Projects Module

_x000D_
_x000D_
import { BrowserModule } from '@angular/platform-browser';_x000D_
import { NgModule } from '@angular/core';_x000D_
import { ProjectsListComponent } from './projects-list.component';_x000D_
import { RouterModule } from '@angular/router';_x000D_
_x000D_
@NgModule({_x000D_
_x000D_
    declarations: [_x000D_
        ProjectsListComponent_x000D_
    ],_x000D_
_x000D_
_x000D_
  imports: [_x000D_
    BrowserModule,_x000D_
    ProjectsListComponent,_x000D_
    RouterModule.forChild([_x000D_
      { path: 'projects', component: ProjectsListComponent }_x000D_
    ])_x000D_
  ]_x000D_
})_x000D_
_x000D_
export class ProjectsModule { }
_x000D_
_x000D_
_x000D_

The process I've taken to setting the module up hasn't been any different to when I was using Angular 2. However, after having issues with compatibility between Angular Cli, firebase and angular fire, I decided to get the latest of everything this morning.

Any help with this one would be massssssively appreciated as I've hit a brick wall with my understanding of it all.

Thank you.

This question is related to angular typescript angular-cli angularjs-ng-model

The answer is


The problem is the import of ProjectsListComponent in your ProjectsModule. You should not import that, but add it to the export array, if you want to use it outside of your ProjectsModule.

Other issues are your project routes. You should add these to an exportable variable, otherwise it's not AOT compatible. And you should -never- import the BrowserModule anywhere else but in your AppModule. Use the CommonModule to get access to the *ngIf, *ngFor...etc directives:

@NgModule({
  declarations: [
     ProjectsListComponent
  ],
  imports: [
    CommonModule,
    RouterModule.forChild(ProjectRoutes)
  ],
  exports: [
     ProjectsListComponent
  ]
})

export class ProjectsModule {}

project.routes.ts

export const ProjectRoutes: Routes = [
      { path: 'projects', component: ProjectsListComponent }
]

In my case, I created a new ChildComponent in Parentcomponent whereas both in the same module but Parent is registered in a shared module so I created ChildComponent using CLI which registered Child in the current module but my parent was registered in the shared module.

So register the ChildComponent in Shared Module manually.


Examples related to angular

error NG6002: Appears in the NgModule.imports of AppModule, but could not be resolved to an NgModule class error TS1086: An accessor cannot be declared in an ambient context in Angular 9 TS1086: An accessor cannot be declared in ambient context @angular/material/index.d.ts' is not a module Why powershell does not run Angular commands? error: This is probably not a problem with npm. There is likely additional logging output above Angular @ViewChild() error: Expected 2 arguments, but got 1 Schema validation failed with the following errors: Data path ".builders['app-shell']" should have required property 'class' Access blocked by CORS policy: Response to preflight request doesn't pass access control check origin 'http://localhost:4200' has been blocked by CORS policy in Angular7

Examples related to typescript

TS1086: An accessor cannot be declared in ambient context Element implicitly has an 'any' type because expression of type 'string' can't be used to index Angular @ViewChild() error: Expected 2 arguments, but got 1 Typescript: No index signature with a parameter of type 'string' was found on type '{ "A": string; } Understanding esModuleInterop in tsconfig file How can I solve the error 'TS2532: Object is possibly 'undefined'? Typescript: Type 'string | undefined' is not assignable to type 'string' Typescript: Type X is missing the following properties from type Y length, pop, push, concat, and 26 more. [2740] Can't perform a React state update on an unmounted component TypeScript and React - children type?

Examples related to angular-cli

Why powershell does not run Angular commands? ERROR in The Angular Compiler requires TypeScript >=3.1.1 and <3.2.0 but 3.2.1 was found instead Angular CLI Error: The serve command requires to be run in an Angular project, but a project definition could not be found Could not find module "@angular-devkit/build-angular" How to remove package using Angular CLI? How to set environment via `ng serve` in Angular 6 Error: Local workspace file ('angular.json') could not be found How do I deal with installing peer dependencies in Angular CLI? How to iterate using ngFor loop Map containing key as string and values as map iteration how to format date in Component of angular 5

Examples related to angularjs-ng-model

Angular CLI - Please add a @NgModule annotation when using latest ng-model for `<input type="file"/>` (with directive DEMO) AngularJS: ng-model not binding to ng-checked for checkboxes filters on ng-model in an input Difficulty with ng-model, ng-repeat, and inputs