su-don’t npm install

Republished with permission from Adam's @medium.

I have this information in a Gist somewhere, but I thought I’d expand on it since it’s a pretty popular issue out there; how to install Node modules globally without needing to resort to sudo. There are suggestions ranging from creating a new group on your machine, “just use nvm”, to just biting the bullet and running with sudo. Isaac suggests changing the ownership of /usr/local to the developer’s account so you no longer need sudo. That is how I used to do it and it totally works and gets the job done.

But what if more than one person uses the same computer? Now who owns /usr/local? Also, have you ever tried to develop on a company owned machine? You might not have the permissions to change permissions.

I’m pretty anal about what I put on my computers and I hate not knowing where something lives in case I ever want to get rid of it. I never remember where global Node modules are installed by default; I always have to look it up (including three or four times while writing this post).

So, here is what a good global npm install setup setup should look like:

  1. I should be able to set it up without any custom or elevated privileges.
  2. I want to tell npm where to put globally installed modules.
  3. I don’t want to use any extra tooling or scripts.
  4. I only want to do this once per machine and never think about it again.

Like most things npm related, the developers already thought ahead for you and there is a feature for that; no new tools required. Everything you need comes from the npm prefix and npm config commands.

For the sake of this post, lets assume you want all your global modules to live inside ~/npm. First, create said directory. This will ensure that the current user has read, write, and execute permissions on ~/npm. Next, we want to tell npm where to save globally install modules. We can do that easily by running:

npm config set prefix ~/npm —g  

This will change the npm global prefix to our newly created directory at ~/npm. You can verify the change by running:

npm prefix -g  

It’s worth emphasizing the -g in both commands. Make absolutely sure you use this switch or this setup will not work properly.

Finally, you’ll need to update your $PATH (or %PATH%) value to include ~/npm/bin. Otherwise, you won’t be able to invoke Node modules from the command line anymore. Setting your path variable varies from operating system to operating system and even shell to shell, so you’re on your own for that, but it is a trivial change. Don’t forget to restart your computer, terminal sessions, or re-source your shell; whatever you need to do to update your $PATH/%PATH% values.

After all that, now you can install modules globally without needing elevated privileges and as a bonus, you know where they are being saved because you made the folder all by yourself with your bare hands!

Run npm install npm -g to update your version of npm to latest. The interesting thing here is that you now have two different versions of npm globally installed on your machine; one under /usr/bin/ and one located under ~/npm/. The former will take precedence when running the npm command, so even if you update npm now, globally, you will still be using the old version of npm that came bundled with Node.

After you’ve reinstalled npm as above, you should be free to delete all of the existing globally installed Node modules and reinstall them to the new location under ~/npm. I just delete them by hand, it’s much faster than doing npm uninstall.

Want to test it out? Here are a few cool modules I have installed globally and use every day:

  • node-inspector — best stand-alone Node debugger out there
  • nodemon — watches for changes in your directory and restarts the server
  • n — version manager that lets you bounce between Node and io versions easily