问题描述
将 Node.js 应用程序(节点 6,npm 5)部署到 Beanstalk 失败:
Deployment of a Node.js application (Node 6, npm 5) to Beanstalk fails with:
gyp 错误!堆栈错误:EACCES:权限被拒绝,mkdir'/tmp/deployment/application/node_modules/heapdump/build'
虽然错误不是特定于包的,但任何 node-gyp 调用都会失败.
though the error is not package-specific, any node-gyp call fails.
AWS 控制台中的 ERROR 事件显示:
The ERROR event in the AWS Console reads:
[实例:i-12345] 命令在实例上失败.返回代码:1 输出:(截断).../opt/elasticbeanstalk/containerfiles/ebnode.py", 行180、在 npm_install 中引发 e subprocess.CalledProcessError: Command'['/opt/elasticbeanstalk/node-install/node-v6.10.0-linux-x64/bin/npm','--production', 'install']' 返回非零退出状态 1. 钩子/opt/elasticbeanstalk/hooks/appdeploy/pre/50npm.sh 失败.更多详细信息,请使用控制台或 EB CLI 检查/var/log/eb-activity.log.
和 eb-activity.log
包含上述 npm 错误.
and eb-activity.log
contained the aforementioned npm error.
该应用程序是通过上传不包含 node_modules
的 .zip 文件手动部署的.IE.它不是通过 eb
命令行工具部署的.
The application was deployed manually by uploading a .zip file that did not include node_modules
. I.e. it was not deployed via the eb
command-line tool.
推荐答案
解决方案
解决办法是将.npmrc
文件添加到应用中,内容如下:
Solution
The solution is to add the file .npmrc
to the application with the content:
# Force npm to run node-gyp also as root, preventing permission denied errors in AWS with npm@5
unsafe-perm=true
(或以任何其他方式配置 npm.(尽管在 /opt/elasticbeanstalk/env.vars
中设置 npm_config_unsafe_perm=true
对我不起作用.)
(Or configuring npm so in any other way. (Though setting npm_config_unsafe_perm=true
in /opt/elasticbeanstalk/env.vars
did not work for me.)
npm install
由 root 用户运行,但它为某些包触发的 node-gyp
进程由默认用户 ec2-user.此用户无法访问由 npm install run 创建并由 root 拥有的
/tmp/deployment/application/node_modules/
目录.(并且它可能也无法访问由相同创建的 /tmp/.npm
和 /tmp/.config
.)通过启用 unsafe-perm
我们强制 npm 也以 root 身份运行 node-gyp,避免了这个问题.
npm install
is run by the root user but the node-gyp
process it triggers for some packages is run by the default user ec2-user
. This user lacks access to the /tmp/deployment/application/node_modules/
directory created by the npm install run and owned by root. (And it likely also lacks access to /tmp/.npm
and /tmp/.config
created by the same.) By enabling unsafe-perm
we force npm to run node-gyp also as root, avoiding the problem.
(我个人更喜欢以
ec2-user
而不是 root
的身份运行,但我想这会更复杂:-))
(Personally I would prefer to run all as
ec2-user
rather than root
but I guess that would be more involved :-))
这篇关于Beanstalk:Node.js 部署 - 由于权限被拒绝,node-gyp 失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!