How to create and publish your npm package Node module in just 10 minutes

Antonio Erdeljac
BlockedUnblockFollowFollowing



Recently I created a JSON-based database and wanted to share it with the world as a package. The main reason I set myself up for that task is that I had never published a proper NPM package before and wanted to challenge myself. In this article, I will show you how to create & publish your first NPM package in under 10 minutes.

Getting started

The good thing about publishing an NPM package is the minimum knowledge required. If you had ever run the following command:

$ npm initCopy the code

and written a default export, you are ready to go. Let’s start by running the command. You should get something similar to this:

The result of npm init -y command

Now let’s create an index.js file with the following code:

A simple function which returns an uppercase version of whatever message we send it. The following project structure should look like this:

The project structure so far

Quite simple, right? That’s pretty much all there is to it. All that is left is to create your npm account over here www.npmjs.com/signup. Then log in through the terminal from your project directory.

Running the npm login command

The last step is to publish our module to the world. But keep in mind that the name specified in your package.json needs to be available.

Changing the npm module name

Now we are ready to publish our module.

npm publishCopy the code

And here it is: www.npmjs.com/package/npm…

You will find yours on your npmjs.com profile page.

Using our package

Now we can install our package in a new project (make sure you create a new project because names will get mixed):

npm i npm-medium-tutorialCopy the code

Now let’s use it:

The result of running our index.js

That ‘s it! Thanks for going through my article. Please leave any questions in responses or email me.

My email: erdeljac DOT antonio AT gmail DOT com

My JSON based DB: github.com/AntonioErde…

My app SwipFeed: play.google.com/store/apps/…