Author: Antonio Erdeljac
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.
The good thing about publishing an NPM package is the minimum knowledge required. If you had ever run the following command:
$ npm init
and written a default export, you are ready to go. Let’s start by running the command. You should get something similar to this:
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:
Quite simple, right? That’s pretty much all there is to it. All that is left is to create your npm account over here https://www.npmjs.com/signup. Then log in through the terminal from your project directory.
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.
Now we are ready to publish our module.
And here it is: https://www.npmjs.com/package/npm-medium-tutorial
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-tutorial
Now let’s use it:
That’s it! Thanks for going through my article. Please leave any questions in responses or email me.