部署至Azure App Service时出现Module not found 'express'问题求助
问题:Azure App Service部署Node.js应用出现"Module not found 'express'"错误
本地运行server.js完全正常,执行npm run build后从dist文件夹启动也无问题,但部署到Azure App Service后出现「Module not found 'express'」错误。以下是相关代码与配置,需分析问题原因并提供解决方法,同时明确是否需要将node_modules和/或package.json纳入部署包。
相关代码与配置
server.js代码
const express = require('express') const compression = require('compression') const path = require('path') const { renderPage } = require('vite-plugin-ssr/server') const isProduction = process.env.NODE_ENV === 'production' const root = path.resolve(__dirname, '..') const clientPath = path.join(root, 'client') async function startServer() { const app = express() app.use(compression()) if (isProduction) { const sirv = require('sirv') app.use(sirv(clientPath)) } else { const vite = require('vite') const viteDevMiddleware = ( await vite.createServer({ root, server: { middlewareMode: true }, }) ).middlewares app.use(viteDevMiddleware) } // eslint-disable-next-line consistent-return app.get('*', async (req, res, next) => { const pageContextInit = { urlOriginal: req.originalUrl, } const pageContext = await renderPage(pageContextInit) const { httpResponse } = pageContext if (!httpResponse) return next() const { body, statusCode, contentType, earlyHints, } = httpResponse if (res.writeEarlyHints) res.writeEarlyHints({ link: earlyHints.map((e) => e.earlyHintLink) }) res.status(statusCode).type(contentType).send(body) }) const port = process.env.PORT || 5173 app.listen(port) console.log(`Server running at http://localhost:${port}`) } startServer()
App Service启动命令
pm2 start js/server.js --no-daemon
GitHub Actions部署流水线配置
name: Azure dev deployment on: push: branches: - main env: AZURE_WEBAPP_NAME: {name} AZURE_WEBAPP_PACKAGE_PATH: './dist' NODE_VERSION: '16.x' jobs: build-and-deploy: runs-on: ubuntu-latest container: node:16 steps: - uses: actions/checkout@v2 - name: set script to remove husky in CI run: npm set-script prepare '' - name: Dependency installation run: npm ci - name: linter run: npm run lint - name: build env: DISABLE_ESLINT_PLUGIN: true run: npm run build --if-present - name: tests env: DISABLE_ESLINT_PLUGIN: true run: npm test - name: 'Deploy to Azure WebApp' uses: azure/webapps-deploy@v2 with: app-name: ${{ appName }} publish-profile: ${{ publishProfile }}
问题原因分析
核心问题是部署包中缺少生产依赖文件:
- 本地运行时,Node.js会从项目根目录的
node_modules加载依赖,即使从dist启动也能向上找到根目录的依赖; - 你的GitHub Actions仅部署了
./dist文件夹,该目录下只有编译后的业务代码,没有package.json和node_modules; - Azure App Service启动时,找不到依赖包的安装信息或实际文件,因此抛出「Module not found」错误。
解决方法
方案1:让Azure自动安装生产依赖(推荐)
此方案无需打包node_modules,仅需将package.json纳入部署包,让Azure自动安装依赖:
- 修改GitHub Actions部署路径:将
AZURE_WEBAPP_PACKAGE_PATH改为项目根目录,确保package.json被包含在部署包中:env: AZURE_WEBAPP_PACKAGE_PATH: '.' # 替换原有的'./dist' - 配置package.json的启动命令:在
scripts中添加start命令,对应你的启动脚本:"scripts": { "build": "你的构建命令", "start": "pm2 start js/server.js --no-daemon" } - 配置Azure App Service启动命令:进入App Service的「配置」→「常规设置」,将启动命令设置为
npm start。Azure会自动检测package.json,并执行npm install --production安装生产依赖。
方案2:将依赖打包进部署包(适合特殊场景)
如果需要直接部署包含依赖的包,可在构建后将package.json和node_modules复制到dist目录:
- 在GitHub Actions中添加复制步骤:在
build步骤后添加以下命令:- name: Copy dependencies and package.json to dist run: | cp package.json ./dist/ cp -r node_modules ./dist/ - 保持原有的
AZURE_WEBAPP_PACKAGE_PATH: './dist'配置即可。
额外注意事项
- 确保
package.json的dependencies字段包含express、compression、sirv、vite-plugin-ssr等生产依赖(devDependencies中的包不会被生产环境安装); - 确认Azure App Service的Node.js版本与本地一致(当前配置为16.x,需在Azure「配置」→「常规设置」中对应设置)。
内容的提问来源于stack exchange,提问作者smuckert
相关产品推荐
相关产品推荐

