Setup your own NPM cache server

If you run npm install a lot you might grow tired of waiting for all the packages to download each time. You might also feel that you are becoming a bit too dependent on the NPM registry to be available at all times.

There is a simple way to get around this - a package called npm-proxy-cache ( This nifty little application is written in Node.JS and gives you a quick and easy way to setup your own cache proxy for NPM. You can run it locally on your computer or set it up on a small virtual server or similar. The latter is useful if you wish to share the cache among your coworkers and perhaps also your build server(s).

Below is a guide that applies to later versions of Ubuntu. Note that this is an example that shows you how to get it running as simple as possible, you might want to do things differently. Especially if you are going to expose it to the internet.

Local setup

Using npm-proxy-cache locally is super easy; you just install and run it.

# Install
npm install -g npm-proxy-cache

# Run (adjust path to fit your system!)
Server setup
# Install Node.JS and NPM
sudo apt-get install -y nodejs npm

# Setup NPM
npm config set prefix '~/.npm-packages'  
export PATH="$PATH:$HOME/.npm-packages/bin"

# Install forever and npm-proxy-cache
npm install -g forever npm-proxy-cache  
ln -s /usr/bin/nodejs /usr/bin/node

# On some distributions (Ubuntu) node is called "nodejs"
ln -s /usr/bin/nodejs /usr/bin/node

# Start
forever ~/.npm-packages/bin/npm-proxy-cache -e -t 600000 -h  

In the last step I tell npm-proxy-cache to bind to IP and to cache each package roughly a week. You can run npm-proxy-cache with --help to see all the available options.

A simple way to make the proxy autostart when the server boots is to use screen and add it to /etc/rc.local (as root) - you might do this differently on your system though.

su - username screen forever ~/.npm-packages/bin/npm-proxy-cache -e -t 600000 -h"  
Utilising the cache

To use the proxy you can either do it inline for a specific call, like this...

npm --proxy --https-proxy --strict-ssl false install gulp  

...or you can configure NPM to always use the proxy, like this:

npm config set proxy  
npm config set https-proxy  
npm config set strict-ssl false  
comments powered by Disqus