Node and npm without sudo

By John Papa

When running npm and node, you may find yourself getting permission errors that ultimately lead you to using `sudo` in your commands. While this helps get around the issue in the short-term, it also places stricter permissions on those installs and it becomes a slippery slope where soon you may need sudo for more than you bargained for. Also, do you really want to be using `sudo` to install npm packages?

After discussing this with Tierney Cyren, we found a much easier way to get node and npm running on my mac than what I used to do a few years back.

First, we want to use the official Node.js install. I mean, its official, right? And if we don’t trust the Node folks with installing Node, then, well, what are we doing here anyway?

Next we create a folder for the global npm packages. I made mine ~/.npm-packages.

Then we use the npm config command to tell npm where we want them.

That’s it!

Detailed steps

Here are the steps, in detail …

  1. Install Node.js from https://nodejs.org/en/download/

  2. Update to the latest version of npm npm install npm -g

  3. Make a new folder for the npm global packages mkdir ~/.npm-packages

  4. Tell npm where to find/store them npm config set prefix ~/.npm-packages

  5. Verify the install

# this should show the versions
node -v  
npm -v  
# this should show npm and ng with no errors
npm list -g --depth=0  

Handling Multiple Node Versions

What happens when a new version of node is released? What if you need version 4.4.2 for one app and 8.9.1 for another? Did you know version 9 is out now too? Yikes! it would be great if we could manage multiple versions of node on the same computer.

Check out this post for more on how to tackle multiple versions of node.

Source:: johnpapa