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

Angular生产构建loadChildren报错:本地正常CI/CD环境解析失败

Fixing Module Parse Error (Unexpected token) in Angular CI/CD Builds

Hey there, let's break down your problem and work through fixes step by step. You mentioned local builds with ng build --prod work perfectly, but when running the CI/CD build command ./node_modules/@angular/cli/bin/ng build --progress false --prod, you hit a module parse error pointing to the dynamic import() syntax in your routing module. Here's what's likely going on and how to resolve it:

1. Check Node.js Version in CI Environment

First off, Angular 8 requires a minimum Node.js version of 10.9.0 (per official documentation). If your CI runner is using an older Node.js version, it won't properly support ES6+ dynamic import() syntax—which is exactly what your error is complaining about. This is the most common culprit here.

  • Fix: Update your CI configuration to use Node.js 10.9.0 or higher (12.x is a safe bet, as it's fully compatible with Angular 8).

2. Align Dependencies Between Local and CI Environments

Local builds work, but CI might be installing different versions of your build dependencies—especially @angular-devkit/build-angular, which handles the actual compilation. Your package.json shows @angular-devkit/build-angular@^0.800.6 paired with @angular/cli@^8.3.20 (a valid match for Angular 8), but discrepancies can happen if you don't lock dependency versions.

  • Fixes:
    • Commit your package-lock.json (if using npm) or yarn.lock (if using yarn) to your repo. This ensures CI installs the exact same dependency versions as your local environment.
    • Use npm ci instead of npm install in your CI setup. npm ci strictly follows the lock file, so no unexpected version changes.

3. Verify TypeScript Configuration

Angular 8's dynamic imports rely on specific TypeScript settings. Double-check your tsconfig.json has these compiler options:

{
  "compilerOptions": {
    "module": "esnext",
    "target": "es2015",
    "moduleResolution": "node"
  }
}
  • module: esnext is critical here—if you've changed this to commonjs or another format, the compiled code won't be parsed correctly during the build.

4. Simplify the CI Build Command

Instead of calling the CLI directly from node_modules, try using your npm script to ensure you're using the project-local CLI version:

npm run build -- --progress false --prod

This avoids conflicts with any global Angular CLI version installed on the CI runner, which might be a different (and incompatible) version.

Bonus: Reproduce the CI Issue Locally

To confirm if the problem is with dependencies/config rather than the CI environment itself, simulate CI steps locally:

  1. Delete your node_modules and dist folders
  2. Run npm ci to mimic CI's dependency install
  3. Execute the CI build command: ./node_modules/@angular/cli/bin/ng build --progress false --prod
    If you get the same error locally, you know it's a project config/dependency issue, not a CI-specific quirk.

Final Notes

Start with checking the Node.js version in CI—it's the quickest fix for this type of error. If that doesn't work, move on to aligning dependencies and verifying your TypeScript config.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 09:17:43