简体   繁体   中英

Error: EACCES: permission denied when running `npm install` on Elastic Beanstalk

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

The package.json is just a:

{
  "dependencies": {
    "node-fetch": "^1.3.3"
  }
}

Running npm install with sudo obviously works, but is preferred to be avoided as a solution.

Setting NPM_CONFIG_PREFIX to a directory at ~ as per npm docs suggestion didn't work either, and the problem persists.

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

This is confusing, as I expected a simple npm install to work out of the box.

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.

I had this problem! You can use ebextensions to create a post-deploy script that changes the permissions of the tmp/npm/.locks folder.

In your node.js project, create a .ebextensions folder if you haven't got one already. Then, add a new config file, eg 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/

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/

Because it's in that post folder, it will be run automatically after your app has deployed -- and hence change the permissions as required.

EDIT: 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/

I have had this problem in past and in my case cleaning the cache fixed my issue. Please try this

npm cache clean

Hope it helps.

The following command will fix the issue. it worked for me.

sudo chown -R 1000:1000 "/home/user/.npm" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM