15

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.

Sbbs
  • 1,610
  • 3
  • 22
  • 34
  • Someone seems to be having the exact same issue on AWS forum as well right now: https://forums.aws.amazon.com/thread.jspa?messageID=758699&tstart=0 – Sbbs Dec 29 '16 at 02:00
  • How did you run the "npm install" with sudo in this Elastic Beanstalk instance? This is not the way, but I would be able to test my App with this temporary workaround. – sergi Dec 29 '16 at 08:12
  • @sergi `sudo /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` – Sbbs Dec 29 '16 at 15:57
  • could you please tell me, are you using github or bitBucket? – Dinu Nicolae Sep 03 '19 at 10:04
  • @DinuNicolae neither, a direct upload deploy. – Sbbs Sep 19 '19 at 22:31

3 Answers3

15

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, 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/

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/
hsriskantha
  • 166
  • 5
0

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.

Mukul Jain
  • 1,121
  • 11
  • 24
0

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

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