Publish a package

In this quick run through, we will see how you can simply publish a package into your Nplfared registry. This will allow you easily share private package for your project and share them with your team 🤝.

Create a package

Let's create a simple package that will be published into our registry.

npm
yarn
pnpm
bun
1npm init

Let's add some code to our package

index.js
1export const add = (a, b) => a + b;
2
3export const sub = (a, b) => a - b;
4
5export const mul = (a, b) => a * b;
6
7export const div = (a, b) => a / b;

Now we need to update our package.json file

package.json
1{
2  "name": "@acme/math",
3  "version": "1.0.0",
4  "description": "",
5  "main": "index.js",
6  "publishConfig": {
7    "access": "public",
8    "registry": "http://your-npflared-registry.dev"
9  },
10  "exports": {
11    ".": "./index.js"
12  },
13}

You need to add a publishConfig property to our package.json file. This property will allow us to specify the registry where we want to publish our package. You will need to replace http://your-npflared-registry.dev with your own registry url.

Generate a token

If you already have a token with dedicated permission you can skip this section

Check this in order to generate a new token

Setup you registry access

In order to authenticate to your registry you will need to add a .npmrc file with the following content

.npmrc
1//your-npflared-registry.dev/:_authToken=YOUR_AUTH_TOKEN

Hit the publish button 🚀

npm
yarn
pnpm
bun
1npm publish

You can now download, upgrade and consume your package 🔥

Installing your package

First make to have setup an .npmrc file with a token having the right accesses.

Then you can run the following command:

npm
yarn
pnpm
bun
1npm install --registry http://your-npflared-registry.dev

If you are using scoped package eg @acme/math you can add the following to your .npmrc file

.npmrc
1@acme:registry=http://your-npflared-registry.dev

In order to automatically resolve your scoped package with your private registry.

Personal note

For example I have multiple package in my private registry scoped with @thomascogez like (@thomascogez/common, @thomascogez/tsconfig @thomascogez/biome-config, ...) That I use in my projects.

In every project want to use them i add the following to my .npmrc file

.npmrc
1@thomascogez:registry=http://your-npflared-registry.dev
2//your-npflared-registry.dev/:_authToken=${NPFLARED_AUTH_TOKEN} # <- NPFLARED_AUTH_TOKEN is stored on my machine env

Publish a new version of your package

If you make change to your package, you will need to publish a new version. This is really easy, you will need to bump the package version in your package.json file and then hit the publish button again !