Npm install failed with "cannot run in wd"

E.H. picture E.H. · Aug 8, 2013 · Viewed 100.8k times · Source

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?

Answer

Dmitry Pashkevich picture Dmitry Pashkevich · Oct 2, 2013

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: