Angular "Unexpected value X imported by the module Y" (AOT) in CI
An NgModule imports array must contain other NgModules or standalone components/directives/pipes. When AOT finds a service, a plain class, or an undefined value there, it rejects the module with "Unexpected value".
What this error means
ng build (AOT) fails with "Unexpected value 'UserService' imported by the module 'AppModule'. Please add a @NgModule annotation" naming the offending value.
Error: Unexpected value 'UserService' imported by the module 'AppModule'.
Please add a @NgModule annotation.Common causes
A service or non-module in imports
A provider (service) belongs in providers, not imports; putting it in imports triggers the unexpected value error under AOT.
An undefined import from a circular or wrong path
A circular import or a wrong path resolves to undefined at module-evaluation time, so imports contains undefined.
How to fix it
Move the value to the correct array
- Read which value and which module the error names.
- Move services to providers, components/directives/pipes to declarations, and only modules or standalone types to imports.
- Rebuild under AOT to confirm.
@NgModule({
imports: [CommonModule, RouterModule],
declarations: [UserCardComponent],
providers: [UserService],
})
export class AppModule {}Break a circular import producing undefined
If the imported value is undefined at evaluation, resolve the circular dependency between the two module files.
How to prevent it
- Keep providers, declarations, and imports arrays distinct and correct.
- Avoid circular imports between module files.
- Build with AOT locally so these surface before CI.