Node.js: Packaging modules for offline deployment using npm-bundle

nodejs-logoIn a production environment, it is common to have restricted internet access on the production deployment hosts.  This means that using the standard ‘npm install’ and pulling modules from the registry.npmjs.org repository is not an option.

Given the breadth of the dependency graph required for most modules, this packaging is something you want automated without needing to modify the package.json file by hand.

After various failed attempts at: using npmbox, scripts wrapping up ‘npm pack’, and archiving the entire node_modules directory – the npm-bundle module finally provided a proper solution.

From Internet Connected Host

In my particular case I needed to package up the elasticdump module, so from the internet connected host:

cd /usr/local/lib/node_modules
npm install elasticdump -g --no-bin-links
npm install npm-bundle -g

Now both elasticdump and the npm-bundle module are installed on the host.  You can verify the installation of the modules and its dependencies with with ‘npm list -g’.

Now let’s create the bundled package of elasticdump 2.4.2 and all its dependencies:

cd elasticdump
npm-bundle

After a bit of time spent downloading, you will have a file in the elasticdump directory named something like ‘elasticdump-2.4.2.tgz’.  This archive can now be copied over to your deployment host (the one that has restricted internet access).

From Internet Restricted Host

Use npm to install from the archive you copied over from the source host:

npm install elasticdump-2.4.2.tgz -g --verbose

You can verify by checking the list of installed modules:

npm list -g

 

REFERENCES

http://51elliot.blogspot.com/2014/08/repackaging-node-modules-for-local.html

http://mdworld.nl/blog/webdevelopment/2015/07/07/npm-without-internet/

https://github.com/npm/npm/issues/4210#issuecomment-210398516