You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular-Universal启动报错:必须传入要引导的NgModule或NgModuleFactory

Fixing "Error: You must pass in a NgModule or NgModuleFactory to be bootstrapped" in Angular Universal

Hey there, I’ve run into this exact snag when setting up Angular Universal with the CLI, so let me walk you through how to fix it.

The core issue here is that AppServerModuleNgFactory isn’t being generated correctly (or at all) during your build, and your server.ts is trying to reference a file that doesn’t exist. Here’s what you need to adjust:

Step 1: Update your server.ts to use the correct bootstrapping approach

Newer Angular Universal versions (especially CLI 10+) prefer using the module directly instead of relying on auto-generated factory files. Replace your factory reference with the actual AppServerModule like this:

import { ngExpressEngine } from '@nguniversal/express-engine';
import * as express from 'express';
import { join } from 'path';
import { AppServerModule } from './src/main.server'; // Import the module, not the factory
import { APP_BASE_HREF } from '@angular/common';

const app = express();

// Point static paths to the browser dist folder
const distFolder = join(process.cwd(), 'dist/your-project-name/browser');
app.use(express.static(distFolder, { index: false }));

// Set up Angular Universal's express engine
app.engine('html', ngExpressEngine({
  bootstrap: AppServerModule, // Use the module directly here
}));

app.set('view engine', 'html');
app.set('views', distFolder);

// Handle all routes with Angular Universal
app.get('*', (req, res) => {
  res.render('index', { 
    req,
    providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }]
  });
});

// Start the server
const port = process.env.PORT || 4000;
app.listen(port, () => {
  console.log(`Node server listening on http://localhost:${port}`);
});

Step 2: Verify your Angular build configuration

Check your angular.json to ensure the server build targets are correctly pointing to your server-side entry file:

"projects": {
  "your-project-name": {
    "architect": {
      "server": {
        "builder": "@angular-devkit/build-angular:server",
        "options": {
          "outputPath": "dist/your-project-name/server",
          "main": "src/main.server.ts",
          "tsConfig": "src/tsconfig.server.json"
        }
      },
      "serve-ssr": {
        "builder": "@nguniversal/builders:ssr-dev-server",
        "options": {
          "browserTarget": "your-project-name:build",
          "serverTarget": "your-project-name:server"
        }
      }
    }
  }
}

Step 3: Double-check your main.server.ts

Make sure this file properly exports the AppServerModule for the server build to pick up:

import { enableProdMode } from '@angular/core';
import { environment } from './environments/environment';
import { AppServerModule } from './app/app.server.module';

if (environment.production) {
  enableProdMode();
}

export { AppServerModule } from './app/app.server.module';
export { renderModule, renderModuleFactory } from '@angular/platform-server';

Why this works

Older Universal setups relied on factory files generated during compilation, but modern Angular CLI builds no longer auto-generate these by default. By switching to bootstrap the AppServerModule directly, you eliminate the dependency on the missing factory file entirely.

After making these changes, rebuild your project with:

ng build && ng run your-project-name:server

Then start your server again, and the error should be resolved.

内容的提问来源于stack exchange,提问作者Saurabh Palatkar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:02:40