Navigate back to the homepage

Publishing a NPM package

Juho Salli
May 31st, 2019 · 2 min read

…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.

1{
2 "name": "digistories-generator",
3 "version": "1.0.0",
4 "src": "src/index.js",
5 "main": "dist/index.js",
6 "devDependencies": {
7 "microbundle": "^0.11.0"
8 }
9}

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.

1cd <your_package_root_dir>/
2npm 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.

1cd <some_other_node_project>/
2npm link <package_name>

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

Now you can just

1import yourcode from "package_name";
2// 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

1cd <your_package_root_dir>/
2npm 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!

More articles from Juho Salli

Automating Jupyter Lab setup

Automating repetative tasks.

December 11th, 2020 · 1 min read

Computing relative dates in pandas

Find the difference of dates in days.

May 1st, 2020 · 1 min read
© 2019–2020 Juho Salli
Link to $https://twitter.com/juhosaLink to $https://github.com/juhosaLink to $https://instagram.com/juhosalliLink to $https://www.linkedin.com/in/juhosalli/