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

部署Express服务到AWS Elastic Beanstalk遇模块缺失问题求助

问题

将Express服务器部署到AWS Elastic Beanstalk时,持续出现模块找不到的错误,使用CodeBuild和buildspec.yml构建并生成构件。

EB部署错误日志

----------------------------------------
/var/log/web.stdout.log
----------------------------------------
Sep 12 22:54:02 ip-172-31-15-80 web: at Function.Module._load (node:internal/modules/cjs/loader:839:12)
Sep 12 22:54:02 ip-172-31-15-80 web: at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
Sep 12 22:54:02 ip-172-31-15-80 web: code: 'MODULE_NOT_FOUND',
Sep 12 22:54:02 ip-172-31-15-80 web: requireStack: [ '/var/app/current/app.js' ]
Sep 12 22:54:02 ip-172-31-15-80 web: }
Sep 12 22:54:02 ip-172-31-15-80 web: node:internal/modules/cjs/loader:959
Sep 12 22:54:02 ip-172-31-15-80 web: throw err;
Sep 12 22:54:02 ip-172-31-15-80 web: ^
Sep 12 22:54:02 ip-172-31-15-80 web: Error: Cannot find module 'express'
Sep 12 22:54:02 ip-172-31-15-80 web: Require stack:
Sep 12 22:54:02 ip-172-31-15-80 web: - /var/app/current/app.js
Sep 12 22:54:02 ip-172-31-15-80 web: at Function.Module._resolveFilename (node:internal/modules/cjs/loader:956:15)
Sep 12 22:54:02 ip-172-31-15-80 web: at Function.Module._load (node:internal/modules/cjs/loader:804:27)
Sep 12 22:54:02 ip-172-31-15-80 web: at Module.require (node:internal/modules/cjs/loader:1028:19)
Sep 12 22:54:02 ip-172-31-15-80 web: at require (node:internal/modules/cjs/helpers:102:18)
Sep 12 22:54:02 ip-172-31-15-80 web: at Object.<anonymous> (/var/app/current/app.js:6:35)
Sep 12 22:54:02 ip-172-31-15-80 web: at Module._compile (node:internal/modules/cjs/loader:1126:14)
Sep 12 22:54:02 ip-172-31-15-80 web: at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
Sep 12 22:54:02 ip-172-31-15-80 web: at Module.load (node:internal/modules/cjs/loader:1004:32)
Sep 12 22:54:02 ip-172-31-15-80 web: at Function.Module._load (node:internal/modules/cjs/loader:839:12)
Sep 12 22:54:02 ip-172-31-15-80 web: at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
...

当前使用的buildspec.yml

version: 0.2

batch:
  fast-fail: true
phases:
  install:
    runtime-versions:
      nodejs: 16
  build:
    commands:
      - npm run build
artifacts:
  type: zip
  files:
    - "**/*"
  base-directory: build
解决方法
  • 核心原因:打包的构件里缺少express等生产依赖,要么是没安装依赖,要么是依赖没被包含进build目录的打包内容里。
  • 调整buildspec.yml方案一:安装并打包生产依赖
    修改install阶段添加生产依赖安装命令,同时确保node_modules被纳入构件:
    version: 0.2
    
    batch:
      fast-fail: true
    phases:
      install:
        runtime-versions:
          nodejs: 16
        commands:
          - npm install --production  # 仅安装生产依赖,减少构件体积
      build:
        commands:
          - npm run build
    artifacts:
      type: zip
      files:
        - "**/*"
        - node_modules/**/*  # 明确包含依赖目录
      base-directory: build
    
  • 调整buildspec.yml方案二:复制依赖到build目录
    如果npm run build会将源码编译到build目录,可在构建后把依赖复制到该目录:
    version: 0.2
    
    batch:
      fast-fail: true
    phases:
      install:
        runtime-versions:
          nodejs: 16
        commands:
          - npm install --production
      build:
        commands:
          - npm run build
          - cp -R node_modules build/  # 把依赖复制到build目录下
    artifacts:
      type: zip
      files:
        - "**/*"
      base-directory: build
    
  • 额外检查项
    • 确认package.json的dependencies字段包含express,不要误放在devDependencies里
    • 如果不想打包node_modules,可移除构件中的node_modules/**/*,但要确保package.json在构件根目录,Elastic Beanstalk会自动执行npm install安装依赖

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 19:15:54