为什么 npm install 仅在 ElasticBeanstalk 中失败?

jeo*_*cha 11 amazon-web-services node.js amazon-elastic-beanstalk nestjs

我有一个 Nest.js (Node.js) 应用程序,我想将其部署在 ElasticBeanstalk(Node 16 版本,AL2 5.5.0)上。我的部署一直失败,我在eb-engine.log.

...
2022/03/23 15:11:48.570759 [INFO] Executing instruction: StageApplication
2022/03/23 15:11:48.570846 [INFO] extracting /opt/elasticbeanstalk/deployment/app_source_bundle to /var/app/staging/
2022/03/23 15:11:48.570860 [INFO] Running command /bin/sh -c /usr/bin/unzip -q -o /opt/elasticbeanstalk/deployment/app_source_bundle -d /var/app/staging/
2022/03/23 15:11:49.274806 [INFO] finished extracting /opt/elasticbeanstalk/deployment/app_source_bundle to /var/app/staging/ successfully
2022/03/23 15:11:49.289272 [INFO] Executing instruction: RunAppDeployPreBuildHooks
2022/03/23 15:11:49.289292 [INFO] Executing platform hooks in .platform/hooks/prebuild/
2022/03/23 15:11:49.289306 [INFO] The dir .platform/hooks/prebuild/ does not exist
2022/03/23 15:11:49.289311 [INFO] Executing instruction: Install customer specified node.js version
2022/03/23 15:11:49.289314 [INFO] installing specified nodejs version...
2022/03/23 15:11:49.289467 [INFO] there is no nodejs version specified in package.json, skip installing specified version of nodejs
2022/03/23 15:11:49.289476 [INFO] Executing instruction: Use NPM to install dependencies
2022/03/23 15:11:49.289484 [INFO] use npm to install dependencies
2022/03/23 15:11:49.289505 [INFO] Running command /bin/sh -c npm config set jobs 1
2022/03/23 15:11:49.574486 [INFO] Running command /bin/sh -c npm --production install
2022/03/23 15:12:06.913580 [ERROR] An error occurred during execution of command [app-deploy] - [Use NPM to install dependencies]. Stop running the command. Error: Command /bin/sh -c npm --production install failed with error signal: killed 
...

Run Code Online (Sandbox Code Playgroud)

我认为在生产模式下安装 npm 包时会发生错误,但我真的很想知道为什么会发生这种情况。我npm --production install在本地计算机上执行,使用完全相同版本的node和npm安装成功。(目前最新的是 Node 16.14.0、npm 8.3.1 - AL2 5.5.0)。

我想知道为什么会发生这种情况以及如何调试更多细节(为什么npm install在elastic beanstalk环境中失败)。

小智 15

我遇到了同样的问题,我能找到解决该问题的唯一方法是增加运行应用程序的实例的大小。为了让它工作,我必须使用t2.medium.

您还可以尝试增加运行应用程序的 EC2 实例的交换,但根据我的经验,这会使部署过程花费太长时间,有时甚至会失败。(如果您想尝试这种方法,请参阅此答案)


pau*_*rtn 7

接受的答案没有解决根本问题:

npm --production install failed with error signal: killed

据我了解,大多数时候这意味着内存泄漏/超时问题。而且npm v7+版本似乎很常见。

解决方案

  1. 使用预构建钩子创建node_modules:

mkdir node_modules

这样做可以阻止 AWS 安装依赖项(因为文件夹 node_modules 已经存在)。

  1. 使用预部署挂钩安装依赖项:

npm install --omit=dev

来源(见pmoleri答案)

AWS Hooks(官方文档)

分步解决方案(来自 AWS 支持)