找不到任何nodejs模块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

如有任何帮助,我们将不胜感激。

您需要在构建步骤中复制node_modules。我这样做的方式是添加一个post_build pahase。

post_build:
commands:
- cp -R node_modules/ dist/node_modules

最新更新