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 [email protected] 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?

0

6 Answers 6

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:

6
  • 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, 2014 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 Mar 31, 2014 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: stackoverflow.com/questions/28763958/…
    – justmoon
    May 12, 2015 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. Jul 18, 2018 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, 2019 at 8:58
89
+50

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.

3
  • 56
    in docker: RUN npm set unsafe-perm true Jul 4, 2018 at 3:05
  • if you are using WSL just type set unsafe-perm true. This will help you Jun 16, 2020 at 7:30
  • That is the best solution!
    – Konrad G
    Dec 9, 2020 at 8:28
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
15

!~~ For Docker ~~!

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

RUN npm set unsafe-perm true
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

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

1
  • This should be the best answer.
    – John Xiao
    Dec 17, 2021 at 7:35

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.