Creating an NPM Cache

April 15, 2015 - 11:22 am

Kept running into issues with npm install failing in CI due to Amazon S3 borkages. This occurred with shocking regularity - we're talking one out of every five builds. I'm sure this was exacerbated by the large number of node modules involved in the node project in question, but S3 has never been a good choice if you need reliability but are unwilling to invest in heavy error checking/handling.

Anyway, clearly I'm not the only one experiencing this, because npm caches are a thing. For my money, I use NPM Lazy. Installation is pretty simple:

mkdir npm-cache
cd npm-cache
npm install npm_lazy

Edit node_modules/npm_lazy/config.js. You'll want to check the URL and potentially proxy sections. In my case, I opted to set up a reverse proxy by way of haproxy, in order to provide http auth style authentication in front of the cache.

Access to the cache is done at the npm level. If you've just got a straight up cache:

npm --registry <whatever>

If you've got a proxy/http auth in front of it:

npm --registry <whatever>
Bonus: SystemD Init

The following assumes you're running npm_lazy by way of NVM under /home/node via a user named node. Alter settings as necessary, of course.

Description=NPM Proxy

ExecStart=/home/node/.nvm/current/bin/node server.js