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

Đang mở Phù hợp với người mới
#34,761 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@0z5a đang làm issue này rồi.

Từ ngày 19/11/2026.

  • #34762 của @0z5a — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
90/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
angular, typescript
Lĩnh vực
build-system, tooling

Hướng nghiên cứu

Bắt đầu tại packages/schematics/angular/utility/standalone/app_component.ts, trong resolveNgModuleBasedData, và kiểm tra cách xử lý các decorator @NgModule không có trường bootstrap. Đọc các kiểm tra hiện có ở gần đó, sau đó xem lại phạm vi kiểm thử của schematic phía máy chủ trong server/index_spec.ts. Hoàn thành khi các decorator không có bootstrap được bỏ qua, hai trường hợp được báo cáo hoạt động như mô tả và các bài kiểm thử tập trung đều đạt.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
TypeScript
Star
27k
Fork
11.8k
Merge trung bình
1 ngày 2 giờ
Pull request đã merge (30 ngày)
225

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của angular/angular-cli

Tất cả issue của angular/angular-cli

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.