How to Publish Your First NPM Package in Minutes!
Simon Pfeiffer
Posted on June 6, 2021
In this article, I’m going to show you how to publish your first NPM package in minutes.
Now the package we’re going to create today is pretty useless, but the process we use is going to be applicable for almost every NPM package that you’re going to want to publish.
In our example today we are going to create two functions, one that takes the nth root of a number, and one that tells us whether a number is even.
Make an NPM Account and Login
To get started, create an NPM account if you haven’t already done so at npmjs.com and make sure to verify your email address. If you don’t verify it, you will not be able to publish any packages.
Now in our command line, we can log in to our npm account:
npm login
You will then be prompted to enter your username, password, and email address.
Create A Package.json
Now the next thing we’re going to do is create our package.json
with
npm init
With the following settings:
- Package Name: Make this something unique(Check the NPM website to see if your name is taken)
- Version: 1.0.0
- Description: This will appear on the NPM page so make it helpful for users
- Entry Point: index.js
- Test Command: You can leave this blank
- GitHub Repository: If you have a GitHub repo for your module, then add it here. This will appear on the NPM page
- Keywords: This will make it easier for developers to find your package
- Author: List yourself
- License: ISC (This is the boilerplate license for open-source software)
Create index.js with nth root function
Now let’s make our first function. Create that index.js
file which we designated as the main file for our package at the root of your project.
For our code, we are going to define module.exports, and whatever we set this value to is what our package will return. For now, let's define a function called nthRt that takes in a number, and then returns the nth root of that number.
module.exports = function nthRt(n) {
return n ** (1/n)
}
NPM Publish and Test It
We can now publish this by running
npm publish
Now if we go back to npm, and click on our packages, we’ll see the package we just published!
Install and Require is-odd
Now let’s get a little fancier. How can we add a dependency? Another similarly useless npm package is the is-odd package, which, you guessed it, tells us if an integer is odd.
https://www.npmjs.com/package/is-odd
Let’s go back to our IDE, and install it with
npm i is-odd
Once that is done, we should see the dependency in our package.json
file.
Then on the top of our index.js
, let's require the module with:
const isOdd = require(‘is-odd’)
Add isEven Function
Now let’s use this isOdd function to create our own function to tell whether a number is even.
Instead of setting module.exports to our nthRt function, let's set it to an object, with keys and values for our individual functions.
Let’s then create our isEven function and just return the negation of our isOdd function.
const isOdd = require(‘is-odd’)
module.exports = {
nthRt: function nthRt(n) {
return n ** (1/n)
},
isEven: function isEven(n) {
return !isOdd(n)
}
}
Add README.md
Now you might have noticed the documentation that was included on the is-odd npm page. To create our own, we just need to make a README markdown file called README.md
We can then insert markdown syntax to explain the installation and usage of our package.
Change Version, Publish, and Test
Now before we can publish this new version, we have to make sure to increment the version value in our package.json()
.
Then run
npm publish
and in a matter of seconds, we should see the new update!
Unpublish the Package
Now, on a final note, you should feel free to test the publishing process until you are comfortable with it, but make sure to unpublish NPM packages that are purely for your testing. We all have to do our part to make the NPM system clean and efficient, so the fewer useless packages there are the better.
To unpublish your package simply run:
npm unpublish <the name of your package> -f
Now if we try to go to our npm page, we’ll get a 404, meaning it was successfully unpublished!
Thanks for reading!
If you have any questions or just want to show off the cool NPM packages that you’re making, comment down below!
Also, don’t forget to follow us on social media to get all the updates on how Codesphere is revolutionizing the development experience.
Happy coding!
Posted on June 6, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.