mugshot

Juho Salli

A software developer and a project manager teaching and doing R&D at a university. Also a husband and a proud dad of one remarkably awesome child.

Find me online:

Publishing a NPM package

May 31, 2019

…in my case was easy as ABC.

Real easy

What

I’ve never published a package to NPM but I have always wanted to try it. Why? The primary reason is to have some idea how one would go about authoring a package. The secondary reason is because all the cool kids do it.

I was working with a simple internal project and saw the opportunity to extract one core piece of it to its own package. The script is almost as simple as it gets; given some input it returns a string.

My initial thoughts about the success rate for this task were neutral, but, being a Finn, they were leaning towards the negative side.

Turns out, any negative thoughts I had were totally wrong.

Testing the package

First things first, let’s have a look the package.json file.

{
  "name": "digistories-generator",
  "version": "1.0.0",
  "src": "src/index.js",
  "main": "dist/index.js",
  "devDependencies": {
    "microbundle": "^0.11.0"
  }
}

With some basic information omitted, this is the entire package.json file of my package.

The name must be unique across the npm-world and main should point to your index javascript file.

You can (and should!) test your script before publishing it. There are a few ways of testing locally, but I chose to use the link command found in npm.

cd <your_package_root_dir>/
npm link

What this does is it installs this local package to your global space. After this command has been executed successfully you can use the link command again but this time in a different project where you will test it.

cd <some_other_node_project>/
npm link <package_name>

The package name is the one you defined in the package.json.

Now you can just

import yourcode from "package_name";
// use your code

This of course depends on your code and what it exports and all the other good stuff. 😏

Now that we have the custom package imported it can be used as you normally would any other package that you had installed with npm install or yarn add.

The really cool thing about this setup is the fact that when I did changes to the script and built it (using yarn microbundle) the app that I used for testing spotted the change and hot-reloaded itself. 🤩

Publishing

Once I was fairly sure that the package would work as I wanted it and it could be used after installing, it was time to publish it. The publishing part was the easiest of the whole process.

After creating an account to npmjs.com and logging in with npm login it was just a matter of calling npm publish.

Then I unlinked the local package with

cd <your_package_root_dir>/
npm unlink

Now the package is published and could be installed with npm install or yarn add. Success!

Conclusion

The time it took to find the right commands and test and publish the package was easily under 60 minutes. Given that this was my first time doing this, that’s a really good time.

There’s a chance of a possible maybe that I’ll do this again!

Further reading

I used these two articles as my sources in this process:

A big thanks to both of the authors!