How to configure a fast NPM Proxy for offline use with Verdaccio

Programming Jun 13, 2020

If you work with JavaScript, you have to use NPM (or Yarn, doesn't really matter), you work on 10 different repositories and you want to add a couple packages. Perhaps express or angular (could be anything), you run a npm install <package name> and go to the  loo, have a chat with your crush (Oath #2 of being engineers, no girlfriends), console her, make a cup of coffee, watch a netflix series and come back to desk, and it's almost finished installing your packages.

NPM is infuriating and painfully slow, here's how can we speed it up

We all know the basic "funda" of NPM; It is a centralized repository from where we can "pull" any package. So, everytime you want to add express to your repository, you have download express again. the npm cli does some smart caching to mitigate this but that's not foolproof.

In order to speed up  NPM, we need to use a "NPM Proxy" or "Self hosted registry". The way it works is quite simple, when you request something from npm, this software intercepts the download and saves a copy on the disk, the next time you request the same module, it serves from disk.

Meet Verdaccio, a stable, battle tested NPM Proxy set to speed up your workflow by 2x

And how do you install a npm registry? Using npm, of course.

npm install -g verdaccio

If you are just trying to speed up your own machine, just run npm config set registry http://localhost:4873 and don't read further.

But if you want some more configuration options and some "enterprise-y" setup, read on.

Use Case Scenarios I will be covering

  1. If you have a fast and reliable connection, npm works pretty well, but since we all are working from home, and you're reading this, I suspect that might not be the case. If your internet goes down for a minute you can't npm install anything.
  2. Another problem with npm is that it is a pain in the ass to deploy in secure environments without internet access. And the domain that I work on, that is almost always the case.

Scenario 1: Your local machine(s) or your office.

If you have multiple devices or are a team, you likely download redundant files. npm install on 5 computers? 5 times the bandwidth. Even if you have super ultra fast internet, you are limited by npm's speed and availability.

If  you have a computer that runs 24x7 (or a server even) then, you should totally use it as a local cache for the entire builing (or LAN).

The setup goes like this

npm i -g verdaccio
mkdir ~/verdaccio # (or any other location, your discretion)
cd ~/verdaccio 
mkdir storage
mkdir config
vim config/config.yml # and Paste the config below
verdaccio --config ~/verdaccio &
steps needed to be performed to setup verdaccio correctly
storage: ../storage
auth:
  htpasswd:
    file: ./htpasswd
uplinks:
  npmjs:
    url: https://registry.npmjs.org/
packages:
  '@*/*':
    access: $all
    publish: $authenticated
    proxy: npmjs
  '**':
    proxy: npmjs
logs:
  - {type: stdout, format: pretty, level: http}
Local config for verdaccio

The advantage of this to minimize time and configuration. On any other machine you need to run the following.

npm config set registry http://<LAN_IP>:4873

The disadvantage would be that, it is not suitable as a private authenticated repository. It is not really secure, no SSL or anything fancy. It doesn't even have replication and thus, not really reliable. But fast and simple.

Scenario 2: Offline Deployment

In this case, you need set up Verdaccio in a reachable host. Configure everything as above. And copy all the files in your existing directory mentioned storage block above. And that's all it is.

Sohan Basak

Hi, I am Sohan. A software engineer by profession, I am really passionate about algorithms, AI/ML, Maths and Physics. Play the guitar as a hobby, the maths behind music is fascinating.