Last updated: Apr 4, 2024
Reading timeยท6 min

The Angular error "NG2011: The component 'X' appears in 'imports', but is not standalone and cannot be imported directly. It must be imported via an NgModule" occurs for multiple reasons:
Ctrl + C and npm start)imports array instead of to your declarations
array by mistake.imports array, e.g. importing MatSnackBar instead of MatSnackBarModule.imports array instead of adding it to the
providers array.
Depending on your Angular version you might get any of the following variations of the same error:
Error: src/app/app.module.ts:26:5 - error NG2011: The component 'X' appears in
'imports', but is not standalone and cannot be imported directly. It must be
imported via a NgModule.
error NG6002: Appears in the NgModule.imports of AppModule, but could not be
resolved to a NgModule class. Is it missing an @NgModule annotation
error NG6002: Appears in the NgModule.imports of AppModule, but itself has errors
The first thing you should try is to restart your Angular server as it might not
be picking up the changes you've made to your imports or declarations arrays
in the @NgModule decorator in app.module.ts.
Ctrl + C (or Cmd + C on macOS)
to stop your development server.
npm start command.npm start
You can also run the npx ng serve command to start your dev server.
npx ng serve
imports arrayAnother common cause of the error is adding a component to your imports array
in @NgModule in app.module.ts.
Here is an example.
// ๐๏ธ importing component import { ExampleComponent } from './example/example.component'; @NgModule({ declarations: [AppComponent], imports: [ BrowserModule, AppRoutingModule, // โ๏ธ Added the component to `imports` by mistake ExampleComponent, ], providers: [], bootstrap: [AppComponent], }) export class AppModule {}
If I start my server, the following error message is shown.
Error: src/app/app.module.ts:26:5 - error NG2011: The component 'ExampleComponent' appears in 'imports', but is not standalone and cannot be imported directly. It must be imported via an NgModule.
To solve the error, make sure to add your components to the declaration array
and not to the imports array.
// ๐๏ธ importing component import { ExampleComponent } from './example/example.component'; @NgModule({ // โ Add the component to the declarations array declarations: [AppComponent, ExampleComponent], imports: [ BrowserModule, AppRoutingModule, ], providers: [], bootstrap: [AppComponent], }) export class AppModule {}
If you still get the error after making the correction, try to restart your
Angular server as it might not be picking up the changes you've made to your
@NgModule decorator.
The error also occurs when you import a component instead of a module, e.g.
MatSnackBar instead of MatSnackBarModule or MatDialog instead of
MatDialogModule and add the component to your imports array.
Here is an example.
// ๐๏ธ importing a component import { MatSnackBar } from '@angular/material/snack-bar'; @NgModule({ declarations: [AppComponent], imports: [ // โ๏ธ trying to add the component to your imports array MatSnackBar, ], providers: [], bootstrap: [AppComponent], }) export class AppModule {}
The code sample above causes the following error.
Error: src/app/app.module.ts:28:5 - error NG6002: 'MatSnackBar' does not appear to be an NgModule class. 70 export declare class MatSnackBar extends _MatSnackBarBase { ~~~~~~~~~~~ This likely means that the library (@angular/material/snack-bar) which declares MatSnackBar has not been processed correctly by ngcc, or is not compatible with Angular Ivy. Check if a newer version of the library is available, and update if so. Also consider checking with the library's authors to see if the library is expected to be compatible with Ivy.
To solve the error, you have to import the module instead of the component and
add the module to your imports array.
// โ import the module import { MatSnackBarModule } from '@angular/material/snack-bar'; @NgModule({ declarations: [AppComponent], imports: [ // โ add the module to your imports array MatSnackBarModule, ], providers: [], bootstrap: [AppComponent], }) export class AppModule {}
Notice that we importing MatSnackBarModule instead of MatSnackBar.
Other examples would be MatDialogModule instead of MatDialog,
HttpClientModule instead of HttpClient, etc.
imports array instead of adding it to providersAnother common cause of the error is adding a service to your imports array
instead of adding it to the providers array in @NgModule.
For example, the following code sample causes the error.
@NgModule({ declarations: [AppComponent], imports: [ // โ๏ธ Services should be in the `providers` array MyService, ], providers: [] }) export class AppModule { }
To solve the error, move your services to the providers array.
@NgModule({ declarations: [AppComponent], imports: [], // โ Works providers: [MyService] }) export class AppModule { }
If the error persists, try to restart your server and clear the cache.
Ctrl + C (or Cmd + C on macOS)
to stop your development server.
npm cache clean --force
npm start command.npm start
You can also run the npx ng serve command to start your dev server.
npx ng serve
node_modules and reinstall your dependenciesIf the error persists, try to stop your development server and reinstall your dependencies.
If you are on macOS or Linux, run the following commands in bash or zsh.
# for macOS and Linux rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # ๐๏ธ clean your npm cache npm cache clean --force # ๐๏ธ install packages npm install
If you are on Windows, run the following commands in CMD.
# for Windows rd /s /q "node_modules" del package-lock.json del -f yarn.lock # ๐๏ธ clean your npm cache npm cache clean --force # ๐๏ธ install packages npm install
Now start your development server.
npm start
You can also run the npx ng serve command to start your dev server.
npx ng serve
importHelpers to true in tsconfig.jsonIf the error persists, open your tsconfig.json file and make sure the
importHelpers property in your compilerOptions object is set to true.
{ "compilerOptions": { "importHelpers": true, } }
The setting allows you to import helper functions from tslib once per project
instead of having to include them per file.
To solve the Angular error "NG2011: The component 'X' appears in 'imports', but is not standalone and cannot be imported directly. It must be imported via an NgModule", make sure:
NgModule.declarations array instead of your imports
array.imports array by mistake.providers array and not to your imports
array.You can learn more about the related topics by checking out the following tutorials: