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

Angular生产环境部署报错求助:Zone.js依赖及语法错误

Fixing Angular Deployment Errors in Production Environment

Hey there, let's tackle those two errors you're facing after modifying the web.config URL rewrite rules. Even though the app runs, these issues can lead to unexpected behavior down the line, so let's fix them properly:

1. Uncaught SyntaxError: Unexpected token < (index):1

This error almost always happens when your server is returning an HTML file (like your index.html) instead of the JavaScript bundles your app needs. Here's why and how to fix it:

  • Problem with URL Rewrite Rules: Your modified rewrite rule might be redirecting requests for static assets (JS, CSS, assets folder) to index.html instead of serving the actual files.
  • Fix: Update your web.config rewrite rule to exclude static resource paths. Add conditions to skip rewriting for files, directories, and specific asset paths:
    <rule name="Angular Routes" stopProcessing="true">
      <match url=".*" />
      <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <!-- Exclude static asset paths from rewrite -->
        <add input="{URL}" pattern="^/(assets|js|css|favicon\.ico|runtime\..*\.js|polyfills\..*\.js|vendor\..*\.js|main\..*\.js)" negate="true" />
      </conditions>
      <action type="Rewrite" url="/index.html" />
    </rule>
    
  • Verify Base Href: Make sure you built your Angular app with the correct base href. For production, run:
    ng build --prod --base-href /
    
    This ensures all resource paths are relative to the root directory, preventing 404s that trigger the HTML fallback.

2. Uncaught Error: In this configuration, Angular requires Zone.js

Even though the app runs, missing Zone.js can break change detection, async operations, and Angular's core functionality over time. Let's resolve this:

  • Check Zone.js Import in Polyfills: Open src/polyfills.ts and confirm this line exists (it's included by default in Angular CLI projects):
    import 'zone.js'; // Included with Angular CLI.
    
  • Ensure Correct Loading Order in index.html: Verify that polyfills.js (which includes Zone.js) loads before main.js in your production index.html:
    <script src="runtime.abc123.js"></script>
    <script src="polyfills.def456.js"></script>
    <script src="styles.ghi789.js"></script>
    <script src="vendor.jkl012.js"></script>
    <script src="main.mno345.js"></script>
    
  • Check Build Optimizations: If you're using build optimizations, ensure they aren't stripping out Zone.js. In your angular.json, under projects.[your-project].architect.build.configurations.production, make sure there's no setting that disables Zone.js accidentally.

After making these changes, redeploy your app to production and clear the browser cache—both errors should be resolved.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 07:41:04