Using NPM Offline - Pt 1

Addy Osmani Addy Osmani

Occasionally, you may need to install npm packages while offline. This could be due to a flaky network connection, being on a flight or during a workshop. Getting this working has been a dream for a while.

They say you should follow your dreams. Unless it's been a while and they haven't followed you back, in which case unfollow them and move on. Luckily, there are a few options available to get npm working offline today.


First, a built-in (but ultimately incomplete) option. In theory, when you want to force installation from the npm cache, you can use the --cache-min flag with a high value. To install a package from the cache, run:

$ npm --cache-min 9999999 install <package-name>

Alternatively, use npm --cache-min Infinity. Typing this out every time you need it is a little tedious, so alias it in your dotfiles. In my bash files, I've previously used:

npmo ="npm --cache-min 9999999 "

In an ideal world, the npm client would just alias this to --offline. So, why isn't this just a default in npm3?

It's the hero Node deserves, but not the one it needs right now

The above --cache-min hack has a number of pretty undesirable shortcomings.

When using it, npm still connects to the registry over the network if a package has dependencies that aren't already in the cache. It will also fail if you install a package that depends on a newer version of a package than the one installed in the past.

The npm team have concerns about shipping a proper offline flag as-is as most users aren't going to understand the subtleties of how the npm cache works. This is understandable. From what I gather, they are open to eventually shipping full support for an offline experience but until then, let's look at some third party stop gap solutions.

Article Source: Using NPM Offline

All credit goes to Addy Osmani.

This text has been copied solely for demonstrative purposes.