Strange error when installing npm package globally on Ubuntu

Installing modules globally has never been a problem for me, neither on Windows nor on Ubuntu. At least until today, when I ran into a somewhat strange problem.

I tried to install the pm2 package on an Ubuntu 12.04 Server (sudo npm install -g pm2@latest), but the package just didn't want to install correctly. I kept getting gyp error messages and this one: "OSError: [Errno 13] Permission denied". I thought it was impossible to get this error since I was running the install command as root (sudo).

Installing modules with the unsafe-perm flag

And after a while I found the solution: using the unsafe-perm flag. sudo npm install -g pm2@latest --unsafe-perm

What the docs say about this flag:

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.

And it worked for me like a charm!

Honestly, I have to admit, that it is not 100% clear to me, why it made any difference in this case. I suspect that it was because of gyp. Does anybody maybe have a better explanation?

Latest blog posts

Generating sound effects with client side JavaScript 6th April 2014

Boilerplate for static site generation 25th March 2014

Automatically loading Grunt tasks with matchdep 11th March 2014

A simple usability trick for Google Maps 10th March 2014

Switching background color of an image with Gimp 2nd March 2014