Just a quick few tips on how to do a ‘proper housekeeping’ routine within npm. These tips eases staying up-to-date with your favorite packages, and at times reclaims disk space; and generally keeps your system tidy.

As of July 21st, 2017 this guide is deprecated.

Note: I’m using OS X as my dev environment.

Before I move forward let me ‘tackle’ an issue I often find with entry and junior level dev’s. I have witnessed recently that most don’t know if they have the latest version of npm or the proper way to install it. I do have an amazing how-to-guide on installing both node.js and npm using Homebrew, though this is a thorough guide; and it even got some love from Forrest Norvell:

I would like to add an updated way to keep everything tidy.

Old method:

brew install node

Note: With this command both node.js and npm will be installed.

The problem with this method is that your npm will be updated only when node.js realases a new version via Homebrew. Not the best way to do it, there is an option to keep npm current once it’s installed with node.js:

sudo npm install npm -g

Yeah using the ‘sudo’ command; ha. This will give you the most current version of npm but let me warn you once node.js is updated via Homebrew, npm will revert to whatever version node.js ships with.

My preferred method:

Let’s assume you installed both node.js and npm via the old method, the following steps will lead you into npm-dev nirvana; ha. Run the following command:

Let’s remove node.js from Homebrew:

brew uninstall node

This will remove node.js from your system, now let’s tidy things up:

brew update && brew upgrade --all && brew cleanup && brew prune && brew doctor

Homebrew will update, it will also upgrade any packages to their latest versions, it will clean old files and it will make sure everything is working. If you get any errors, make sure you take care of them before moving forward.

Now, enter the following command:

brew install node --without-npm

Node.js will be installed but this time without npm. You won’t have any issues moving forward, every time node.js is updated via Homebrew it will be upgraded without npm.

Now let’s install npm:

curl -L https://www.npmjs.com/install.sh | sh

The final step is to add ~/.node/bin to your PATH so commands you install globally are usable. I added this line to my ~/.path script, which gets run via ~/.bash_profile.

export PATH="$HOME/.node/bin:$PATH"

You’re welcome, you’ll be running now updated versions of node.js and npm without conflicting with one another.

Installing and Upgrading NPM Packages

Another question I’m often asked by junior dev’s is, “what is the proper way to install and upgrade npm packages?” This is a tough one to answer; no it really is. You can go around a room full of dev’s with various npm work experience and I bet you they’ll answer differently. Here are a few ways to do it according to npm documentation:

Installing Packages

npm install -g npm@latest

Note: -g stands for ‘global’ I often find that junior dev’s leave out the -g and the packages are installed locally. Run the following commands to see what I mean.

Global:

npm list -g --depth=0

All global installed npm packages should show.

Locally:

npm list --depth=0

All locally installed npm packages should show. Having locally installed packages is not an issue but can cause a problem later; I’ll leave that one for you to find out the hard way.

If you need to install a specific version of an npm package heres how to do it:

npm install -g npm@2

This will install whatever version you may need, enter the name of the package, and after the @ enter the version you need.

Now, if you want to know if you have any outdated packages in your dev environment, before I show you how to find out and upgrade. I want to warn you that if you upgrade it may break things within your build.

Outdated NPM packages:

npm outdated -g

and if you want to do find out locally:

npm outdated

For example, let’s assume the following npm package is outdated coffee-script, and we need to upgrade it:

npm update -g coffee-script

There you go.

Finally, over time npm can accumulate a large cache to keep things tidy I suggest running the following command every thirty days:

npm cache clean

Happy coding and cheers.