Recently I had the need to try and create some npm packages and somehow struggled to work out what to do. So I figured I'd write-up a quick guide on how to publish to a private npm registry.

Goal

In this article I aim to cover the following:

Create a node package.

Publish the package to a private registry.

Install the package in another project. I won't go down any particular best practices, partially because my aim is to show you how to get something working fast, and mostly because I have no idea what the best practices are.

Requirements

To follow along with this article you're going to need the following:

node installed locally.

a private npm registry - I use myget.org

somewhere to edit your javascipt.

Publishing

For a package to be hosted in a private registry it needs to have a scope, if for example I was to create a "secret-eslint" package for my private "jonathan-hart" registry, the package name would end up being: @jonathan-hart/secret-eslint

We can then install the package into our other projects using:

npm install @jonathan-hart/secret-eslint

Ace! The trick is getting everything setup so that when npm sees the @jonathan-hart scope it knows to go publish to/install from our private registry, we'll do this using the .npmrc files. But more on that later!

So lets go right back to the beginning, create an empty folder, fire up the command line and type:

Once this has run you see a shiny new packages.json file like this:

{ "name" : "@jonathan-hart/secret-eslint" , "version" : "1.0.0" , "description" : "" , "main" : "index.js" , "scripts" : { "test" : "echo \"Error: no test specified\" && exit 1" }, "keywords" : [], "author" : "" , "license" : "ISC" }

Let's add a super-simple index.js at the root to give us something to work with:

module .exports = function ( ) { console .log( 'Wow it\'s working!' ); }

Now we need to tell npm where the @jonathan-hart scope belongs. To do this we've going to need the registry and auth-token (you should be able to find these somewhere on your private registry host). Once you have these create a .npmrc file at the root of the project like this:

= https://www.myget.org/F/jonathan-hart/npm/ = true : _authToken=b166d613-58b3-4888-96e3-96136110eb52

Obviously replace the scope, feed url, and auth-token with your own values. We should now be ready to publish the package! To do that we can simply run the publish command:

npm publish

Boom! Your package should either now be published, or you should have a console full of errors that I have no idea how to fix (good luck).

Installing

So now we've published our private @jonathan-hart/secret-eslint package it's time to pull it into another project and give it ago! Create another empty folder, open up the command line and run:

npm init --yes

This should create a new projects.json file, now it's time to install our new package. If we try and install the package straight away npm will complain that it's unable to find the @jonathan-hart scope, so first we need to tell npm how to resolve the scope. To do this simply copy-paste the .npmrc file from the package, if your publish is working fine then this should just work. Now lets install that package:

npm install @jonathan-hart/secret-eslint

Now create index.js with the following:

const secret = require ( '@jonathan-hart/secret-eslint' ); secret();

Then run it:

node index

You should now see "Wow it's working" printed to the console. Congratulations you did it!

From here it should be pretty straight forward to start adding some real functionality to your package.