ng add @angular/ssr fails with TypeError when an NgModule in the app module file has no bootstrap array

Ouverte Adaptée aux débutants
#34,761 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

@0z5a y travaille déjà.

Depuis le 19/11/2026.

  • #34762 par @0z5a — ouverte

Évaluation

Difficulté
2/5
Temps estimé
1-3 heures
Accessibilité débutants
90/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
Active
Stack technique
angular, typescript

Piste de recherche

Commence dans packages/schematics/angular/utility/standalone/app_component.ts, dans resolveNgModuleBasedData, et examine comment sont traités les décorateurs @NgModule sans champ bootstrap. Lis les vérifications existantes à proximité, puis examine la couverture du schematic du serveur dans server/index_spec.ts. Le travail est terminé lorsque les décorateurs sans bootstrap sont ignorés, que les deux cas signalés se comportent comme décrit et que les tests ciblés réussissent.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

area: @schematics/angular
Command

add

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

I think before the server schematic started inferring the app component name (commit ea1143dd, April 2025). I have not run an older release to confirm.

Description

ng add @angular/ssr (and ng generate server) crashes on an NgModule-based app when one of the @NgModule decorators in the file that main.ts bootstraps has no bootstrap array. The cases I hit:

  1. The root module uses ngDoBootstrap() instead of bootstrap: [...] (Angular Elements apps, AngularJS hybrid apps that bootstrap through UpgradeModule).
  2. The file declares another @NgModule without bootstrap (for example a small shared module) before the root module.

In packages/schematics/angular/utility/standalone/app_component.ts, resolveNgModuleBasedData loops over the module's @NgModule decorators and does:

const bootstrapProp = getMetadataField(node, 'bootstrap').find(...);
const componentIdentifier = (bootstrapProp?.initializer as ts.ArrayLiteralExpression)
  .elements[0] as ts.Identifier | undefined;

When bootstrapProp is undefined, the optional chain stops at initializer and .elements is read from undefined. The function is meant to return null in that case: the caller in server/index.ts already falls back to App / AppModule when nothing is found. So the schematic stops with a TypeError instead of using the fallback (case 1) or the right module (case 2).

Minimal Reproduction
  1. ng new demo --no-standalone
  2. In src/app/app-module.ts, remove bootstrap: [App] and add ngDoBootstrap() {} to AppModule.
  3. ng add @angular/ssr

Or, for case 2, keep bootstrap: [App] and put this above AppModule in the same file:

@NgModule({ exports: [] })
export class SharedModule {}

I reproduced this with SchematicTestRunner against @schematics/angular 22.3.0 (workspace -> application --standalone=false -> edit the module -> server and ssr schematics). Both schematics throw for case 1, and server throws for case 2. The unmodified app works.

Exception or Error
TypeError: Cannot read properties of undefined (reading 'elements')
    at resolveNgModuleBasedData (node_modules/@schematics/angular/utility/standalone/app_component.js:65:14)
Your Environment
@schematics/angular 22.3.0
@angular-devkit/schematics 22.3.0
Node.js 22.23.5
Anything else relevant?

Skipping decorators that have no bootstrap field (continue, like the ts.isObjectLiteralExpression check just above it) is enough to fix both cases: case 2 then finds AppModule, and case 1 falls through to the existing fallback. I have a patch with two tests in server/index_spec.ts and can open a PR.

Langage dominant
TypeScript
Étoiles
27k
Forks
11.8k
Merge moyen
1 j 2 h
PR mergées (30 j)
225

Préparer son environnement

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de angular/angular-cli

Toutes les issues de angular/angular-cli

Issues similaires

Plus d'issues TypeScript

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.