DEV Community

Cover image for How to publish a package on NPM
Georgi Yanev
Georgi Yanev

Posted on • Originally published at blog.georgi-yanev.com on

How to publish a package on NPM

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 .
Enter fullscreen mode Exit fullscreen mode

✨ Run npm init

npm init
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

… 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
}
Enter fullscreen mode Exit fullscreen mode

📢 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
Enter fullscreen mode Exit fullscreen mode

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

npm login
Enter fullscreen mode Exit fullscreen mode

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

npm publish
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

🆙 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
Enter fullscreen mode Exit fullscreen mode

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!

Top comments (0)