Azure App Service中编译后的TypeScript项目无法启动求助
问题背景
我有一个基于Node.js/TypeScript/Express的REST API服务(集成Swagger文档),本地编译运行正常,但部署到Azure App Service后无法启动。目标是最终构建dist目录作为独立Node.js服务运行,当前需先解决启动问题。
package.json的scripts配置
"scripts": { "build": "npx tsc --build", "start": "node dist/server.js --port 8080", "dev": "nodemon ./src/server.ts", "lint": "eslint --ignore-path .eslintignore --ext .js,.ts", "format": "prettier --ignore-path .gitignore --write \"**/*.+(js|ts|json)\"", "test": "mocha -r ts-node/register 'src/**/*.spec.ts'" },
启动错误日志
Enter the source directory to make sure the script runs where the user expects
2023-11-15T13:57:37.520261569Z cd "/home/site/wwwroot"
2023-11-15T13:57:37.520265269Z
2023-11-15T13:57:37.520268770Z export NODE_PATH=/usr/local/lib/node_modules:$NODE_PATH
2023-11-15T13:57:37.520272870Z if [ -z "$PORT" ]; then
2023-11-15T13:57:37.520276570Z export PORT=8080
2023-11-15T13:57:37.520280470Z fi
2023-11-15T13:57:37.520283970Z
2023-11-15T13:57:37.520287570Z npm start
2023-11-15T13:57:38.128405653Z npm info using npm@9.6.4
2023-11-15T13:57:38.128753158Z npm info using node@v18.17.1
2023-11-15T13:57:38.165803018Z
2023-11-15T13:57:38.165824618Z > src@1.0.0 start
2023-11-15T13:57:38.165830518Z > node dist/server.js --port 8080
2023-11-15T13:57:38.165834418Z
2023-11-15T13:57:38.302706285Z node:internal/modules/cjs/loader:1080
2023-11-15T13:57:38.302769386Z throw err;
2023-11-15T13:57:38.302776286Z ^
2023-11-15T13:57:38.302791386Z
2023-11-15T13:57:38.302795386Z Error: Cannot find module 'cors'
2023-11-15T13:57:38.302799287Z Require stack:
2023-11-15T13:57:38.302802987Z - /home/site/wwwroot/dist/server.js
2023-11-15T13:57:38.302806987Z at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
2023-11-15T13:57:38.302810987Z at Module._load (node:internal/modules/cjs/loader:922:27)
2023-11-15T13:57:38.302815187Z at Module.require (node:internal/modules/cjs/loader:1143:19)
2023-11-15T13:57:38.302818987Z at require (node:internal/modules/cjs/helpers:121:18)
2023-11-15T13:57:38.302822787Z at Object.(/home/site/wwwroot/dist/server.js:16:32)
2023-11-15T13:57:38.302835787Z at Module._compile (node:internal/modules/cjs/loader:1256:14)
2023-11-15T13:57:38.302839587Z at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
2023-11-15T13:57:38.302843187Z at Module.load (node:internal/modules/cjs/loader:1119:32)
2023-11-15T13:57:38.302847087Z at Module._load (node:internal/modules/cjs/loader:960:12)
2023-11-15T13:57:38.302851187Z at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
2023-11-15T13:57:38.302855387Z code: 'MODULE_NOT_FOUND',
2023-11-15T13:57:38.302859287Z requireStack: [ '/home/site/wwwroot/dist/server.js' ]
2023-11-15T13:57:38.302863388Z }
2023-11-15T13:57:38.306441642Z
2023-11-15T13:57:38.306461842Z Node.js v18.17.1
2023-11-15T13:57:36.126Z INFO - 18-lts_20230908.2.tuxprod Pulling from appsvc/node
2023-11-15T13:57:36.139Z INFO - Digest: sha256:f968f5d09a5e36f4e447bb8e3073f284b3ecc4bb60cf72d1243c35dc6fd5b29e
2023-11-15T13:57:36.140Z INFO - Status: Image is up to date for mcr.microsoft.com/appsvc/node:18-lts_20230908.2.tuxprod
2023-11-15T13:57:36.153Z INFO - Pull Image successful, Time taken: 0 Minutes and 0 Seconds
2023-11-15T13:57:36.171Z INFO - Starting container for site
2023-11-15T13:57:36.172Z INFO - docker run -d --expose=8080 --name app-strata-open-cms-uksouth-demo_0_28bf6b29 -e WEBSITE_SITE_NAME=app-strata-open-cms-uksouth-demo -e WEBSITE_AUTH_ENABLED=False -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=app-strata-open-cms-uksouth-demo.azurewebsites.net -e WEBSITE_INSTANCE_ID=bd0010bcc7f2a648cafc4bcac18f1660692939e3ec29400aeb60d8a3d788c1c3 -e WEBSITE_USE_DIAGNOSTIC_SERVER=True appsvc/node:18-lts_20230908.2.tuxprod
2023-11-15T13:57:36.172Z INFO - Logging is not enabled for this container.Please use https://aka.ms/linux-diagnostics to enable logging to see container logs here.
2023-11-15T13:57:36.549Z INFO - Initiating warmup request to container app-strata-open-cms-uksouth-demo_0_28bf6b29 for site app-strata-open-cms-uksouth-demo
2023-11-15T13:57:39.424Z ERROR - Container app-strata-open-cms-uksouth-demo_0_28bf6b29 for site app-strata-open-cms-uksouth-demo has exited, failing site start
2023-11-15T13:57:39.437Z ERROR - Container app-strata-open-cms-uksouth-demo_0_28bf6b29 didn't respond to HTTP pings on port: 8080, failing site start. See container logs for debugging.
2023-11-15T13:57:39.441Z INFO - Stopping site app-strata-open-cms-uksouth-demo because it failed during startup.
错误核心
部署后启动时找不到cors模块,导致容器退出,站点启动失败。
解决方案
确认依赖安装方式:确保
cors已作为生产依赖安装,执行命令:npm install cors --save若之前误装为开发依赖,先卸载再重新安装:
npm uninstall cors --save-dev && npm install cors --save检查Azure部署配置:
- 若通过CI/CD流程部署,需在脚本中添加生产依赖安装命令:
npm install --production - 若采用Zip部署,确保本地已安装所有生产依赖,且
node_modules目录包含在部署包内
- 若通过CI/CD流程部署,需在脚本中添加生产依赖安装命令:
优化启动脚本:Azure会自动设置
PORT环境变量,无需硬编码,修改start脚本:"start": "node dist/server.js"同时在代码中读取环境变量:
const port = process.env.PORT || 8080; app.listen(port, () => { console.log(`Server running on port ${port}`); });验证构建产物:本地执行
npm run build后,运行npm run start确认服务正常,确保构建过程无依赖遗漏
内容的提问来源于stack exchange,提问作者user3067684

