Skip to content
Latchkey

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.

ng build
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

  1. Read which value and which module the error names.
  2. Move services to providers, components/directives/pipes to declarations, and only modules or standalone types to imports.
  3. Rebuild under AOT to confirm.
app.module.ts
@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.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →