Angular 17部署后出现MIME类型错误及语法错误求助
问题描述
开发Angular 17应用,部署后页面出现以下错误:
Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.
main-PI7SWQGV.js:1 Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.
scripts-EEEIPNC3.js:1 Uncaught SyntaxError: Unexpected token '<' (at scripts-EEEIPNC3.js:1:1)
Node Express和IIS服务器部署均出现相同问题,以下是项目配置和构建命令:
angular.json配置
{ "$schema": "./node_modules/@angular/cli/lib/config/schema.json", "version": 1, "newProjectRoot": "projects", "projects": { "qadashboardfe": { "projectType": "application", "schematics": { "@schematics/angular:component": { "style": "scss" } }, "root": "", "sourceRoot": "src", "prefix": "app", "architect": { "build": { "builder": "@angular-devkit/build-angular:application", "options": { "outputPath": "dist/qadashboardfe", "index": "src/index.html", "browser": "src/main.ts", "polyfills": [ "zone.js" ], "aot": true, "tsConfig": "tsconfig.app.json", "inlineStyleLanguage": "scss", "assets": [ "src/favicon.ico", "src/assets" ], "styles": [ "node_modules/bootstrap/scss/bootstrap.scss", "node_modules/bootstrap-icons/font/bootstrap-icons.css", "src/styles.scss" ], "scripts": [ "node_modules/bootstrap/dist/js/bootstrap.bundle.min.js" ], "server": "src/main.server.ts", "prerender": true, "ssr": { "entry": "server.ts" }, "allowedCommonJsDependencies": [ "highcharts" ] }, "configurations": { "production": { "budgets": [ { "type": "initial", "maximumWarning": "3mb", "maximumError": "5mb" }, { "type": "anyComponentStyle", "maximumWarning": "2kb", "maximumError": "4kb" } ], "outputHashing": "all" }, "development": { "optimization": false, "extractLicenses": false, "sourceMap": true } }, "defaultConfiguration": "production" }, "serve": { "builder": "@angular-devkit/build-angular:dev-server", "configurations": { "production": { "buildTarget": "qadashboardfe:build:production" }, "development": { "buildTarget": "qadashboardfe:build:development" } }, "defaultConfiguration": "development" }, "extract-i18n": { "builder": "@angular-devkit/build-angular:extract-i18n", "options": { "buildTarget": "qadashboardfe:build" } }, "test": { "builder": "@angular-devkit/build-angular:karma", "options": { "polyfills": [ "zone.js", "zone.js/testing" ], "tsConfig": "tsconfig.spec.json", "inlineStyleLanguage": "scss", "assets": [ "src/favicon.ico", "src/assets" ], "styles": [ "node_modules/bootstrap/scss/bootstrap.scss", "node_modules/bootstrap-icons/font/bootstrap-icons.css", "src/styles.scss" ], "scripts": [ "node_modules/bootstrap/dist/js/bootstrap.bundle.min.js" ] } } } } }, "cli": { "analytics": "d65730db-8bc1-4d78-aaef-715dc5dc12b9" } }
构建命令
ng build --configuration production --output-path dist/qedashboardfe --base-href /qedashboardfe/
问题根源
这类错误本质是服务器未正确映射静态资源路径,请求JS文件时返回了HTML页面(通常是404页面),导致浏览器将HTML当作JS解析,触发MIME类型错误和语法错误。
解决步骤
1. 匹配构建输出与部署目录
构建命令指定输出目录为dist/qedashboardfe,部署时需确保:
- Express:静态文件目录指向该文件夹,而非父目录
- IIS:站点物理路径设置为
dist/qedashboardfe,而非上层dist
2. 修正Express配置
确保Express正确处理SPA路由和静态资源,示例配置:
const express = require('express'); const path = require('path'); const app = express(); // 静态资源指向构建产物 app.use('/qedashboardfe', express.static(path.join(__dirname, 'dist/qedashboardfe'))); // SPA fallback:所有非API请求返回index.html app.get('/qedashboardfe/*', (req, res) => { res.sendFile(path.join(__dirname, 'dist/qedashboardfe', 'index.html')); }); app.listen(3000, () => console.log('服务器运行在3000端口'));
注意:/qedashboardfe需与--base-href设置完全一致。
3. 修正IIS配置
- 将站点物理路径指向
dist/qedashboardfe文件夹 - 安装
URL Rewrite模块,在web.config添加SPA路由规则:
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="Angular路由" stopProcessing="true"> <match url=".*" /> <conditions logicalGrouping="MatchAll"> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="/qedashboardfe/index.html" /> </rule> </rules> </rewrite> <staticContent> <!-- 确保JS文件MIME类型正确 --> <mimeMap fileExtension=".js" mimeType="application/javascript" /> </staticContent> </system.webServer> </configuration>
- 检查IIS是否已配置
.js文件的MIME类型为application/javascript(默认存在,缺失则手动添加)
4. 验证base-href正确性
构建时设置了--base-href /qedashboardfe/,需确保:
- 访问地址为
http://你的域名/qedashboardfe,而非直接访问根路径 - 服务器路由映射和静态资源路径均包含该子路径
5. 检查构建产物完整性
重新执行构建命令,确认dist/qedashboardfe目录下包含index.html、main-*.js、scripts-*.js等所有必要文件,无遗漏。
内容的提问来源于stack exchange,提问作者Venugopal

