npm install -g — ERR! Please try running this command again as root/Administrator.

When running npm install -g somepackage, you may get an EACCES error asking you to run the command again as root/Administrator. Here’s how to fix this on OS X.

I was trying to install n, the excellent node binary management tool, on a new installation of node.js and got the following error:

npm ERR! Error: EACCES, mkdir '/usr/local/lib/node_modules/n'
npm ERR!  { [Error: EACCES, mkdir '/usr/local/lib/node_modules/n']
npm ERR!   errno: 3,
npm ERR!   code: 'EACCES',
…
npm ERR! Please try running this command again as root/Administrator.

This is a permissions issue and the fix, courtesy of Tim Schaub, is to recursively change the owner of the files in your /usr/local folder to the current user:

sudo chown -R $USER /usr/local

Why shouldn’t I use sudo?

Read the ‘what, no sudo?’ section in Isaac Schlueter’s introduction to npm.

Also, a note to JetBrains…

If your permissions are not set correctly, you will also run into this issue if you use the WebStorm or IntelliJ IDEA IDEs from JetBrains (my editor of choice is Sublime Text 2 but I do evaluate other IDEs all the time) when creating a new project based on the Node.js Express App template. I hope the Intellij folks handle this more gracefully in the future (ideally by allowing you to fix the permissions problem from inside the app by entering your password and clicking a button).