Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Verdaccio - Host Your Own NPM Packages

This tutorial is for those of you who wish to develop and use NPM packages, but don't wish to share them with the rest of the world. It will show you how to set up a private Verdaccio server that will authenticate against a generated .htaccess file. However, in future I will show you how to authenticate against your private Gitlab server, but if your impatient, you can following this.

Steps

Create a docker-compose.yml file with the following content

version: '2.1'
services:
  verdaccio:
    image: verdaccio/verdaccio:3.8
    container_name: verdaccio
    restart: always
    ports:
      - "4873:4873"
    volumes:
        - "./volumes/verdaccio/storage:/verdaccio/storage"
        - "./volumes/verdaccio/conf:/verdaccio/conf"

Create the volumes:

mkdir -p $HOME/volumes/verdaccio/storage
mkdir -p $HOME/volumes/verdaccio/conf

Download the configuration file.

cd $HOME/volumes/verdaccio/conf
wget https://raw.githubusercontent.com/verdaccio/docker-examples/master/docker-local-storage-volume/conf/config.yaml

Create a htpasswd file in the same place (online tool). This will be the username and password you need to use when adding your registry later.

vim $HOME/volumes/verdaccio/conf/htpasswd

Install docker-compose if you haven't already.

Start Verdaccio through docker-compose:

docker-compose up

Your verdaccio server is now up. If you go the following address in your browser, you will see it, with some instructions on how to add a user, and to publish:

http://[IP or Domain]:4873

Testing - Create, Publish, and Install A Package

Skip this if you don't feel the need to test that your Verdaccio server is up and running. However, the commands here do show you the differences you need to make when trying to push/pull from your private repository instead of the default NPM one.

mkdir my-tiny-package
cd my-tiny-package

Create a package.json file within the folder with the following contents.

{
    "name": "@myCompanyName/my-tiny-package",
    "version": "1.0.0"
}

Authenticate against the registry with:

npm adduser --registry  http://[IP or Domain]:4873

Publish the package with:

npm publish --access=private --registry http://192.168.16.26:4873

Now when you want to install the package:

mkdir my-project
cd my-project
mkdir node_modules
npm install --registry  http://192.168.16.26:4873 @myCompanyName/my-tiny-package

References

Last updated: 28th December 2018
First published: 31st October 2018

This blog is created by Stuart Page

I'm a freelance web developer and technology consultant based in Surrey, UK, with over 10 years experience in web development, DevOps, Linux Administration, and IT solutions.

Need support with your infrastructure or web services?

Get in touch