Strange error when installing npm package globally on Ubuntu

10th February 2014 | by Adam Beres-Deak | npm, ubuntu, node.js

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?

by Adam Beres-Deak

Latest blog posts

Worth watching: Douglas Crockford speaking about the new good parts of JavaScript in 2014 20th October 2014

Automatically adjusting the height of a textarea to its content text 16th October 2014

Loading webfonts with high performance on responsive websites 8th October 2014

Loading web pages really fast - optimistic page loading with Instantclick.io 8th October 2014

Generating sound effects with client side JavaScript 6th April 2014