Georgi's Blog

How to publish a package on NPM

January 30, 2019 ⌛ 3 min read

A couple of weeks ago I created and published my first node package on NPM and it was a lot of fun. Much to my surprise, it was much easier than I thought.

There are at least a few boilerplate and scaffolding tools to help you get started, and while they probably are the way to go, because they come with all the tooling, tests and hooks, here’s a simple example of actually publishing a node package. There are plenty of guides on how to do it, including this article right on npm’s docs, but here’s one way on how to get a minimum setup going on.

🔁 Make a new repository on GitHub

Create a new repository and clone it locally on your machine. Then change directory into it and open the folder with your favorite editor.

git clone https://github.com/jumpalottahigh/how-to-publish-to-npm
cd how-to-publish-to-npm/
code .

✨ Run npm init

npm init

You can also pass -y to auto say yes to every question in the initializer. I’d say you probably want to review the information, so maybe go slower. Because we just want to see how things work, the defaults will suffice.

Check your package.json and make sure that the main key points to your entry point for the module. For now, let’s make it point to index.js.

📦 Write the module

Create index.js,

touch index.js

… and let’s export a simple function called sum, that adds two numbers together and returns the result.

exports.sum = function(a, b) {
  return a + b
}

📢 Publish the package

Stage, commit and push all your changes to the remote repository on GitHub.

git add .
git commit -m "Export a sum function"
git push origin master

Next, you need to login to npm, so run:

npm login

After you have gone through the flow, you are ready to publish your package using:

npm publish

That’s it, it’s indeed that easy! Go to npmjs.com and search for your newly published package by the name you gave it.

🍴 Consume the module

Last but not least, let’s try it out. In your favorite project of choice install the package:

npm i how-to-publish-to-npm

Then use it in code as:

// Importing the module using a named import
import { sum } from 'how-to-publish-to-npm'

console.log(sum(7, 11)) // 18

🆙 Upgrading the package

Say you make some changes to your function and want to release a new version. You can use npm version to bump the package version.

npm version major # 1.0.0
npm version minor # 0.1.0
npm version patch # 0.0.1

Read more about bumping a package version.

Then you can release the package again with the new version, running npm publish.

🌯 Wrap up

That’s all of it! Congrats 🎉! It is really not as complicated as one may think (or at least I did). I hope this inspires you to author your own modules and to share them with the Node community.

Also, please don’t ship modules like that to production. For the sake of creating a proof of concept (PoC) and just getting something out there, we heavily overlooked testing and continuous integration. There’s nothing stopping you from starting small and bringing in all the bells and whistles when you need them or starting with a generated project. You do you!

Banggood promo

Did I miss something?

Ask a question and see it updated in the article, alongside with credit for you.

Question/suggestion:

Thanks for your suggestion!

Support the blog:

I hope you find my content helpful. If you'd like to support the blog, you can do so by clicking the affiliate links below before making a purchase. This comes at no additional cost to you.
Or you can buy me a coffee via paypal and support me on Patreon.

🎁 FPVTIPS T-SHIRT GIVEAWAY 🎁

We are excited to announce a t-shirt giveaway!

To enter the giveaway, subscribe to the FPVTIPS newsletter or to the FPVTIPS YouTube channel! That's all! The drawing will be on October 1st, 2019.

And yes, subscribing to both does double your chances of winning.

📰 Monthly Newsletter

Get the latest scoop of what's new in the FPV drone racing and freestyle world. What to watch, who to follow and more.

I hate spam, and so I don't spam. Unsubscribe at any time.

Need even more FPV in your life? Check out my YouTube channels:

FPVTIPS YouTube channel
GeorgiFPV YouTube channel
Drone, Georgi flying FPV and coding work station
Banggood promo