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

Angular 6 Universal构建报错:无法解析server/main.bundle文件

Angular 6 Universal: Module not found Error for ./dist/server/main.bundle when running npm run build:SSR

Hey there, let's tackle this error you're facing with your Angular 6 Universal migration. I've seen this issue pop up often when moving from Angular 5 to 6, mostly due to changes in default build paths and bundle naming conventions. Here's how to fix it step by step:

Root Cause Breakdown

Angular 6 overhauled the default output structure for Universal builds—they dropped the .bundle extension from server output files, and migration steps can sometimes leave misaligned paths in your config files or server.ts.

Fix 1: Update the Bundle Path in server.ts

The error directly points to a missing file at ./dist/server/main.bundle, which Angular 6 no longer generates. Open your server.ts file and locate the line importing the server bundle:

  • Replace this outdated path:
    const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./dist/server/main.bundle');
    
  • With this Angular 6-compatible path:
    const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./dist/server/main');
    

Angular 6 outputs main.js instead of main.bundle, so removing the .bundle extension (or explicitly using .js) will resolve the path mismatch.

Fix 2: Verify angular.json Output Paths

Angular 6 uses angular.json instead of the old .angular-cli.json. Double-check your config to ensure build paths are correct:

  1. Open angular.json and navigate to projects.[your-project-name].architect.server.options
  2. Confirm outputPath is set to dist/server (this is the default, but migrations can occasionally overwrite this)
  3. Also verify the browser build path at projects.[your-project-name].architect.build.options.outputPath is set to dist/browser

Fix 3: Correct Your package.json Build Scripts

Your build:SSR script might be using outdated syntax or missing critical steps. Update the scripts section in package.json to:

"scripts": {
  "build:browser": "ng build --prod",
  "build:server": "ng run [your-project-name]:server:production",
  "build:SSR": "npm run build:browser && npm run build:server"
}

Replace [your-project-name] with the actual name of your project from angular.json. This ensures both browser and server bundles are fully built before the server tries to reference them.

Fix 4: Clean Old Build Artifacts

Leftover files from your Angular 5 build can cause conflicts. Delete the dist folder entirely and rebuild:

  • On Windows: rmdir /s dist
  • On macOS/Linux: rm -rf dist
    Then run npm run build:SSR again.

Fix 5: Ensure Dependencies Are Up to Date

Make sure you have the Angular 6-compatible Universal packages installed. Run these commands to update:

npm install @nguniversal/express-engine@6 @nguniversal/common@6 --save

If you follow these steps, that module not found error should be resolved. Feel free to reach out if you hit any unexpected snags!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:54:05