Skip to content

environment is undefined since CLI 19.2.7 #30091

Closed as not planned
Closed as not planned
@jnizet

Description

@jnizet

Command

serve

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

19.2.6

Description

In order to be able to customize an app for various profiles, we use the CLI environments to specify component types, and imports those component types from the environment in other components.

For example, we have

import { ChildComponent } from '../app/child/child.component';
import { ChildrenComponent } from '../app/children/children.component';

export const environment = {
  childrenComponent: ChildrenComponent,
  childComponent: ChildComponent
};

and

@Component({
  selector: 'app-root',
  imports: [
    environment.childrenComponent
  ]
  // ...
})
export class AppComponent {
}

This breaks at runtime in development since version 19.2.7 of the CLI, most probably because of the component metadata added to the chunks. Apparently, the generated Javacript code tries to access the environment before it has been defined.

This does not happen for all such cases: only apparently when a component imported from the environment itself tries to import another component from the environment.

Minimal Reproduction

See https://stackblitz.com/edit/stackblitz-starters-g8kmhvqk?file=src%2Fmain.ts for a complete minimal reproduction.

The code compiles fine, but at runtime we immediately get the following error:

Exception or Error

Uncaught TypeError: Cannot read properties of undefined (reading 'childComponent')
    at children.component.ts:6:25
    at children.component.ts:6:40

Your Environment

Angular CLI: 19.2.7
Node: 20.19.0
Package Manager: npm 10.8.2
OS: linux x64

Angular: 19.2.6
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, router

Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.1902.7
@angular-devkit/core         19.2.7
@angular-devkit/schematics   19.2.7
@angular/build               19.2.7
@angular/cli                 19.2.7
@schematics/angular          19.2.7
rxjs                         7.8.2
typescript                   5.8.3
zone.js                      0.15.0

Anything else relevant?

Running ng serve --configuration production works fine.
A workaround consists in replacing

imports: [environment.childComponent]

by

imports: [forwardRef(() => environment.childComponent)]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions