Trying Verdaccio

2021-06-11

Today we are going to explore creating a private repo. I want to be able to test the possibility of building npm packages that I just need locally.

Lets install veradaccio

npm install --global verdaccio

To run it

>verdaccio

The application runs a local server on localhost:4873. That was quick!

Okay... well lets get a package to publish to it!

Thta was simple I guess this blog post will take a tangent and look into how to publish a package. This is something I haven't quite done yet before.

To get things started we're going to create a new project folder, a new git project and package. We'll call it something really creative like custom-package

Lets init the npm package. Since my eventual goal is to have a series of packages that are part of a global system, it makes sense to me that this should all be under some common scope. So we'll initialize the package with a scope

npm init --scope=@kaashin

Alright so now to add the package we need to add the verdaccio registry that is running on localhost.

We can do this by either:

npm install --registry http://localhost:4873

or edit the .npmrc file with

//.npmrc
registry=http://localhost:4873

Next, with the default installation we don't have a user. So we'll run the following command to create a user on the local verdaccio server

npm adduser --registry http://localhost:4873

It will prompt you for a username , password , and email which you fill out for registration.

Next lets, try publishing this basically empty package.

npm publish --registry http://localhost:4873

// or since we added it to the .npmrc, its already set to the right registry
npm publish

And...

Seems too simple...

Lets stretch this experiment out a bit more. Lets modify the code of the index.js just a bit to actually export something. And we'll make a new test project that will consume this package.

Inside the index.js lets add the following

function helloWorld() {
  console.log("Hello World");
}

module.exports = {
  helloWorld,
};

I guess before we go on and create the test project, lets first run the publish command again.

Oops. Rookie mistake after working on projects where versioning wasn't really my priority.

Lets update the package.json version to 0.1.1 and publish it again.

Perfect. Got it.

So now in a really simple new project, I'm going initialize it with npm and try adding adding the package @kaashin\02-custom-package . Not sure why I named it this way... but we'll use it as is.

yarn add @kaashin\02-custom-package

Is that really it?!

So lets create a simple index.js script that requires the helloWorld function from the package!

const customModule = require("@kaashin/02-custom-package");

customModule.helloWorld();

And now when I execute this node file, "Hello World" is printed which was from the original custom package.

This was surprisingly really easy to do and setup!

Next steps

Exploring the possibility to host this on a remote server so I can use it anywhere. Perhaps follow this article https://medium.com/verdaccio/setting-up-verdaccio-on-digitalocean-61b5d08e4f0d