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 your package.json
:
"config": {
"unsafe-perm":true
}
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:
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.
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
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
@Alexander Mills answer - just made it easier to find:
RUN npm set unsafe-perm true
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With