错误:EACCES:在Elastic Beanstalk上运行`npm install`时权限被拒绝 [英] Error: EACCES: permission denied when running `npm install` on Elastic Beanstalk
问题描述
我已经通过Elastic Beanstalk设置了一个默认的clean node.js应用程序,并且有一个node.js脚本试图在项目目录(/var/app/current/deploy-dist
)中运行npm install
,但是,抛出以下权限错误:
I've provisioned a default clean node.js app via Elastic Beanstalk, and have a node.js script trying to run npm install
inside the project directory (/var/app/current/deploy-dist
), however, the following permission error is thrown:
npm WARN locking Error: EACCES: permission denied, open '/tmp/.npm/_locks/staging-f212e8d64a01707f.lock'
npm WARN locking at Error (native)
npm WARN locking /tmp/.npm/_locks/staging-f212e8d64a01707f.lock failed { Error: EACCES: permission denied, open '/tmp/.npm/_locks/staging-f212e8d64a01707f.lock'
npm WARN locking at Error (native)
npm WARN locking errno: -13,
npm WARN locking code: 'EACCES',
npm WARN locking syscall: 'open',
npm WARN locking path: '/tmp/.npm/_locks/staging-f212e8d64a01707f.lock' }
npm WARN deploy-dist No description
npm WARN deploy-dist No repository field.
npm WARN deploy-dist No license field.
npm ERR! Linux 4.4.35-33.55.amzn1.x86_64
npm ERR! argv "/opt/elasticbeanstalk/node-install/node-v6.9.1-linux-x64/bin/node" "/opt/elasticbeanstalk/node-install/node-v6.9.1-linux-x64/bin/npm" "install"
npm ERR! node v6.9.1
npm ERR! npm v3.10.8
npm ERR! path /tmp/.npm/_locks/staging-f212e8d64a01707f.lock
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall open
npm ERR! Error: EACCES: permission denied, open '/tmp/.npm/_locks/staging-f212e8d64a01707f.lock'
npm ERR! at Error (native)
npm ERR! { Error: EACCES: permission denied, open '/tmp/.npm/_locks/staging-f212e8d64a01707f.lock'
npm ERR! at Error (native)
npm ERR! errno: -13,
npm ERR! code: 'EACCES',
npm ERR! syscall: 'open',
npm ERR! path: '/tmp/.npm/_locks/staging-f212e8d64a01707f.lock' }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.
npm ERR! Please include the following file with any support request:
npm ERR! /var/app/current/deploy-dist/npm-debug.log
package.json
只是一个:
{
"dependencies": {
"node-fetch": "^1.3.3"
}
}
使用sudo运行npm install
显然可以,但是最好避免使用它作为解决方案.
Running npm install
with sudo obviously works, but is preferred to be avoided as a solution.
根据 npm
将NPM_CONFIG_PREFIX
设置为~
上的目录文档建议也不起作用,问题仍然存在.
Setting NPM_CONFIG_PREFIX
to a directory at ~
as per npm
docs suggestion didn't work either, and the problem persists.
我怀疑问题出在/tmp/.npm
的权限不正确
I suspect the problem lies in incorrect permissions for /tmp/.npm
, which are
drwxr-xr-x 114 root root 4.0K Dec 27 17:04 .npm
这令人困惑,因为我期望一个简单的npm install
可以开箱即用.
This is confusing, as I expected a simple npm install
to work out of the box.
更新:不应认为项目目录已经包含一个node_modules
文件夹,但是即使将其删除并运行npm install
也不能解决该问题.
UPDATE: Should not that the project directory already contains a node_modules
folder, but even removing it and running npm install
doesn't fix it.
推荐答案
我遇到了这个问题!您可以使用 ebextensions 来创建
I had this problem! You can use ebextensions to create a post-deploy script that changes the permissions of the tmp/npm/.locks
folder.
在您的node.js项目中,如果尚未创建一个.ebextensions
文件夹.然后,添加一个新的配置文件,例如00_create_postdeploy_script.config
,并带有以下yaml:
In your node.js project, create a .ebextensions
folder if you haven't got one already. Then, add a new config file, e.g. 00_create_postdeploy_script.config
, with the following yaml:
files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/99_fix_node_permissions.sh":
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
chown -R nodejs:nodejs /tmp/.npm/_locks/
部署时,这将在/opt/elasticbeanstalk/hooks/appdeploy/post
中创建一个名为99_fix_node_permissions.sh
的脚本,如下所示:
When you deploy, this will create a script in /opt/elasticbeanstalk/hooks/appdeploy/post
called 99_fix_node_permissions.sh
, which looks like this:
#!/usr/bin/env bash
chown -R nodejs:nodejs /tmp/.npm/_locks/
由于它位于该post
文件夹中,因此它将在您的应用程序部署后自动运行-从而根据需要更改权限.
Because it's in that post
folder, it will be run automatically after your app has deployed -- and hence change the permissions as required.
:如果您在整个 .npm
文件夹的权限上遇到问题,则应将配置文件的最后一行更改为:
If you're having trouble with the permissions of the whole .npm
folder, then you should change the last line of the config file to:
chown -R nodejs:nodejs /tmp/.npm/
这篇关于错误:EACCES:在Elastic Beanstalk上运行`npm install`时权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!