Npm install failed with "cannot run in wd"

124,642

Solution 1

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

Solution 2

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:

Solution 3

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.

Solution 4

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

Solution 5

!~~ For Docker ~~!

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

RUN npm set unsafe-perm true
Share:
124,642

Related videos on Youtube

E.H.
Author by

E.H.

Updated on November 10, 2021

Comments

  • E.H.
    E.H. over 2 years

    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?

  • E.H.
    E.H. about 10 years
    Sorry I didn't see this until now. I tried the "unsafe-perm" before but it didn't work either. The problem still exists
  • Dmitry Pashkevich
    Dmitry Pashkevich about 10 years
    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
  • justmoon
    justmoon about 9 years
    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/…
  • Alexander Mills
    Alexander Mills almost 6 years
    in docker: RUN npm set unsafe-perm true
  • android.weasel
    android.weasel almost 6 years
    '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.
  • fulv
    fulv over 5 years
    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.
  • Lukas Knuth
    Lukas Knuth over 4 years
    The --unsafe-perm also works for npm publish. This is useful when publishing packages from CI, where builds in containers often run as root.
  • Stas Panyukov
    Stas Panyukov almost 4 years
    if you are using WSL just type set unsafe-perm true. This will help you
  • Konrad G
    Konrad G over 3 years
    That is the best solution!
  • John Xiao
    John Xiao over 2 years
    This should be the best answer.
  • Mayur
    Mayur about 2 years
    It worked for me also, thanks @Undistraction