Node.js

Using NPM Offline - Pt 3

Addy Osmani Addy Osmani

This is a continuation of Using NPM Offline.

npm_lazy

Another popular lazy caching proxy for npm is npm_lazy. Similar to local-npm, once setup any modules requested that aren't in the local cache are fetched from the npm registry lazily.

In npm_lazy, the only things cached are package index metadata and package tarfiles. Pretty much all other endpoints get transparently proxied. This means an npm install for cached packages will always work and (mostly the same as local-npm), more exotic endpoints won't work if the main registry or mirrors are down - they'll act as their non-npm_lazy equivalents.

To install:

$ npm install -g npm_lazy

To start the server, run:

$ npm_lazy

Similar to local-npm, the registry can be set to the local server running as follows:

$ npm config set registry http://localhost:8080/

npm_lazy has a relatively decent level of configuration, allowing customisation of the lifespan of the cache, maximum retries, HTTP timeouts and whether HTTPS requests are checked against Node's list of certificate authorities.

Alternatives

There have been numerous other packages written that aim to offer a caching proxy for npm (such as Sinopia), but I've tried to keep the list short to avoid choice paralysis.

Bundling

In this post I've looked at offline package installation, but you may also be interested in offline package bundling. This is where you want to bundle a package and all of its dependencies into a single archive for sharing with a system that might be offline. This comes up a lot in workshops.

There's an open issue on npm to try improving the bundling story around npm pack and bundledDependencies. Until this comes to fruition, I recommend trying out either:

  • Freight - can bundle both npm and bower dependencies into a compressed archive

  • npmbox - similarly supports creating/installing dependencies from a single archive

  • bundle-dependencies - deeply bundles all module dependencies for a package into a monolithic package you would later publish to npm


Article Source: Using NPM Offline

All credit goes to Addy Osmani.

This text has been copied solely for demonstrative purposes.