Search code examples
angularangular-standalone-componentsangular19

Angular 19 - Component AppComponent is standalone, and cannot be declared in an NgModule


On my Angular v19 app I get the following error:

Component AppComponent is standalone, and cannot be declared in an NgModule

or

'imports' is only valid on a component that is standalone.

It worked perfectly in v18 before the update.

What happened?


Solution

  • In v19, Angular swapped the default value for standalone from false to true. It means that every component declared is now implicitly standalone.

    If your app relies on NgModule that declare components/pipes/directives, you will need to add standalone: false to all of those:

    @Component({
      standalone: false,  // this is now required when using NgModule
      ...
    })
    export class AppComponent {}
    
    @NgModule({
      declaration: [AppComponent], 
      ...
    })
    export class AppModule {}
    

    Also, as part of the migration/update experience, the Angular team provides a migration schematic to update your apps and take care of most of the breaking changes. For this you only need to run ng update.

    Make sure to also have a look at the update guide.