202

I am trying to get my node environment set up on a new Ubuntu 12.04 instance, with Node 0.8.14 already installed, but I ran into problems when I try to run npm install. So when I try npm install, it says that I need to run it as root or adminisrator:

Error: EACCES, mkdir '/usr/local/lib/node_modules/coffee-script'
npm ERR!  { [Error: EACCES, mkdir '/usr/local/lib/node_modules/coffee-script']
npm ERR!   errno: 3,
npm ERR!   code: 'EACCES',
npm ERR!   path: '/usr/local/lib/node_modules/coffee-script',
npm ERR!   fstream_type: 'Directory',
npm ERR!   fstream_path: '/usr/local/lib/node_modules/coffee-script',
npm ERR!   fstream_class: 'DirWriter',
npm ERR!   fstream_stack: 
npm ERR!    [ 'DirWriter._create                 (/usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:36:23)',
npm ERR!      '/usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53',
npm ERR!      'Object.oncomplete (fs.js:297:15)' ] }
npm ERR! 
npm ERR! Please try running this command again as root/Administrator.

But when try to run it as sudo, it says the following:

npm WARN cannot run in wd PackNodeDev@0.0.1-166 npm install -g coffee-script node-gyp (wd=/home/ubuntu/PackNode)

In my package.json, it contains the following scripts:

"scripts": {
    "preinstall": "npm install -g coffee-script node-gyp",
    "start": "node server.js",
    "test": "mocha --require should --compilers coffee:coffee-script --colors"
 },

The rest of devdependencies are valid since I have been installing it all right on my own machine (Mac) Does anyone have a clue why this is happening?

Dmitry Pashkevich
  • 13,431
  • 9
  • 55
  • 73
E.H.
  • 3,271
  • 4
  • 19
  • 18

6 Answers6

285

The documentation says (also here):

If npm was invoked with root privileges, then it will change the uid to the user account or uid specified by the user config, which defaults to nobody. Set the unsafe-perm flag to run scripts with root privileges.

Your options are:

  1. Run npm install with the --unsafe-perm flag:

    [sudo] npm install --unsafe-perm
    
  2. Add the unsafe-perm flag to your package.json:

    "config": {
        "unsafe-perm":true
    }
    
  3. Don't use the preinstall script to install global modules, install them separately and then run the regular npm install without root privileges:

    sudo npm install -g coffee-script node-gyp
    npm install
    

Related:

Community
  • 1
  • 1
Dmitry Pashkevich
  • 13,431
  • 9
  • 55
  • 73
  • 2
    Sorry I didn't see this until now. I tried the "unsafe-perm" before but it didn't work either. The problem still exists – E.H. Mar 31 '14 at 19:47
  • 11
    This works for me: `sudo npm install --unsafe-perm`, however `sudo npm install` doesn't, although I added `"unsafe-perm":true` to package.json... Not sure why – Dmitry Pashkevich Mar 31 '14 at 19:58
  • 9
    Adding it to the "config" property in package.json actually sets "npm_package_config_unsafe_perm" so option 2 doesn't work. See: http://stackoverflow.com/questions/28763958/nodejs-unsafe-perm-not-working-on-package-json – justmoon May 12 '15 at 19:21
  • 1
    'unsafe-perm': true failed for me too. Shame it didn't give the error and context (including its uid change) in the error message, instead of dumbing the cause out of existence and giving something cryptic, surprising, and hostile. – android.weasel Jul 18 '18 at 11:20
  • While `npm install --unsafe-perm` works for me, I tried to follow the implication about changing the default `user` config. So I did `npm set user my_user` and `npm set group my_group`, which adds the corresponding entries in the root user's `.npmrc` file. But the problem is that the `node_modules` folder itself and its subfolders are still owned by root, so that doesn't help. I couldn't figure out any way to have them not be owned by root. – fulv Feb 14 '19 at 08:58
  • The `--unsafe-perm` also works for `npm publish`. This is useful when publishing packages from CI, where builds in containers often run as root. – Lukas Knuth Nov 04 '19 at 14:07
89

The only thing that worked for me was adding a .npmrc file containing:

unsafe-perm = true

Adding the same config to package.json didn't have any effect.

Undistraction
  • 42,754
  • 56
  • 195
  • 331
31

I have experienced the same problem when trying to publish my nodejs app in a private server running CentOs using root user. The same error is fired by "postinstall": "./node_modules/bower/bin/bower install" in my package.json file so the only solution that was working for me is to use both options to avoid the error:

1: use --allow-root option for bower install command

"postinstall": "./node_modules/bower/bin/bower --allow-root install"

2: use --unsafe-perm option for npm install command

npm install --unsafe-perm
Yassine Khachlek
  • 1,134
  • 12
  • 17
15

!~~ For Docker ~~!

@Alexander Mills answer - just made it easier to find:

RUN npm set unsafe-perm true
Yitzchak
  • 3,303
  • 3
  • 30
  • 50
14

OP here, I have learned a lot more about node since I first asked this question. Though Dmitry's answer was very helpful, what ultimately did it for me is to install node with the correct permissions.

I highly recommend not installing node using any package managers, but rather to compile it yourself so that it resides in a local directory with normal permissions.

This article provides a very clear step-by-step instruction of how to do so:

https://www.digitalocean.com/community/tutorials/how-to-install-an-upstream-version-of-node-js-on-ubuntu-12-04

E.H.
  • 3,271
  • 4
  • 19
  • 18
3

I fixed this by changing the ownership of /usr/local and ~/Users/user-name like so:

sudo chown -R my_name /usr/local

This allowed me to do everything without sudo

Daniel O'Leary
  • 159
  • 2
  • 6