Npm install failed with "cannot run in wd"
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:
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 tonobody
. Set theunsafe-perm
flag to run scripts with root privileges.
Your options are:
-
Run
npm install
with the--unsafe-perm
flag:[sudo] npm install --unsafe-perm
-
Add the
unsafe-perm
flag to yourpackage.json
:"config": { "unsafe-perm":true }
-
Don't use the
preinstall
script to install global modules, install them separately and then run the regularnpm 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
Related videos on Youtube
E.H.
Updated on November 10, 2021Comments
-
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 trynpm 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. about 10 yearsSorry 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 about 10 yearsThis works for me:
sudo npm install --unsafe-perm
, howeversudo npm install
doesn't, although I added"unsafe-perm":true
to package.json... Not sure why -
justmoon about 9 yearsAdding 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 almost 6 yearsin docker:
RUN npm set unsafe-perm true
-
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 over 5 yearsWhile
npm install --unsafe-perm
works for me, I tried to follow the implication about changing the defaultuser
config. So I didnpm set user my_user
andnpm set group my_group
, which adds the corresponding entries in the root user's.npmrc
file. But the problem is that thenode_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 over 4 yearsThe
--unsafe-perm
also works fornpm publish
. This is useful when publishing packages from CI, where builds in containers often run as root. -
Stas Panyukov almost 4 yearsif you are using WSL just type set unsafe-perm true. This will help you
-
Konrad G over 3 yearsThat is the best solution!
-
John Xiao over 2 yearsThis should be the best answer.
-
Mayur about 2 yearsIt worked for me also, thanks @Undistraction