Using NPM Offline - Pt 2
This is a continuation of Using NPM Offline.
A significantly more robust solution to
--cache-min is Nolan Lawson's offline-first local-npm package (which I love). It's a Node server that acts like a local npm mirror (without needing to do a complete replication of the whole npm registry).
npm installs are fetched from the registry and then modules and their deps get stored in a local PouchDB database. This caches them so subsequent
npm installs use the local cache rather than calling to the network.
local-npm also takes care of keeping modules updated when they change. It does this by listening for changes to the remote registry so you don't have to worry about staleness.
local-npm installed, run:
$ npm install -g local-npm
npm is built on top of CouchDB.
local-npm replicates the skimdb part of this database to a local PouchDB instance. Running:
will begin the replication process. There's no need to wait for the complete skimdb download (which can take a few hours). It will fall back to the network for any libraries that aren't yet replicated. As mentioned,
local-npm is offline-first. It has upfront replication for metadata and tarballs download the first time you install a specific version.
To complete setup, you'll want to go ahead and set
npm to point to the local server
local-npm is running:
$ npm set registry http://127.0.0.1:5080
Awesome. You should now be able to open up a new tab and just
npm install any dependencies you require using the caching proxy.
Here's what a successful offline
npm install should look like:
and for posterity, here's a tab where I'm running
local-npm and we can see it correctly proxying through requests for both the
To test everything works as expected:
- Check your network connection is up
- Run an npm install to grab a module or collection of dependencies from a package.json file. For example, lodash.
- At this point, local-npm will have a cached version of these modules stored
- Clear the npm cache using
npm cache cleanor delete the modules from your node_modules directory
- Turn off your wifi/kill your network connection
- Try running that
npm installagain. Everything should install correctly through the local-npm cache without hitting the network at all
A handy extra that comes with
local-npm's server setup is a simple in-browser UI for browsing local modules and searching for them. You can access this at http://localhost:5080/_browse.
Publishing packages from local-npm doesn't require any change of registry configuration. Just ensure you're online and everything should work as expected.
As a reminder, in case you need to switch back to the main registry or your preferred mirror for any reason just use npm set once again:
$ npm set registry https://registry.npmjs.org
Although local-npm is pretty great, it's not specifically the direction the npm team want to head in for their offline support. Regardless, it's still a pretty solid stop gap for today and I strongly recommend giving it a shot.
Since publishing this post, Nolan has also shared some speed tests comparing local-npm to regular npm. In short, regular is faster the first time you npm install a package, but after this local-npm is consistently faster by an order of 2-3 times.
Using local-npm with other tools
I tested local-npm with Yeoman and Yahoo's generator-fluxible, which only uses npm for package management. As you can see below, everything including dependency installation worked fine and I was able to preview the output from a cached install without issues:
Article Source: Using NPM Offline
All credit goes to Addy Osmani.
This text has been copied solely for demonstrative purposes.