April 27, 2016

NPM proxy cache for CI

Same like bower, it might happen that you need to build things not only locally but also in some sort of CI. Deploying a npm proxy cache is fairly easy.

Installation

I suggest to add a specific user for this:

useradd -m -d /var/cache/npm -s /bin/false npm

Some neccessary packages:

apt-get install nodejs

You probably want to fetch the latest packages.

Now we install the cache:

sudo -u npm /bin/bash
cd
npm install npm-proxy-cache

Startup

/etc/systemd/system/npm-proxy-cache.service

[Unit]
Description=npm proxy cache
After=network.target
After=syslog.target

[Service]
RuntimeDirectory=npm-proxy-cache
PIDFile=/var/run/npm-proxy-cache/npm-proxy-cache.pid
WorkingDirectory=/var/cache/npm
ExecStart=/var/cache/npm/node_modules/npm-proxy-cache/bin/npm-proxy-cache -e -h %H
Restart=always
RestartSec=10
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=npm-proxy-cache
User=npm
Group=npm

[Install]
WantedBy=multi-user.target

Client usage

npm config set proxy http://npm-proxy-cache.example.com:8080
npm config set https-proxy http://npm-proxy-cache.example.com:8080
npm config set strict-ssl false