How To Create And Publish An NPM Package

Introduction

In this article, we will see step by step guide to create and publish an npm package.

Create and Publish npm package

Let us see step by step procedure to create and publish our first npm package.

Step 1

Before we get started, make sure you create an account on npm and sign-in with your credentials.

Step 2

Make sure you have GitHub account. If not, create a GitHub account and create one repository.

Step 3

In your local machine, create one folder and include these files. Edit them as per your logic and naming conventions. Here, I’m creating a package to remove space from a given string. In the end, your package should perform something and it should be helpful for someone.

index.js

module.exports = function removespace(string) {
    if (typeof string !== "string") throw new TypeError("Meh! Enter a string!!");
    return string.replace(/\s/g, "");
};

package.json

{
    "name": "@pranambhat/space",
    "version": “1.0 .0”,
    "description": "Removes space from the given string",
    "keywords": ["npm", "space", "package"],
    "homepage": "https://github.com/PranamBhat/npm-package",
    "bugs": {
        "url": "https://github.com/PranamBhat/npm-package/issues",
        "email": "[email protected]"
    },
    "main": "index.js",
    "scripts": {
        "test": "mocha"
    },
    "publishConfig": {
        "access": "public"
    },
    "repository": {
        "type": "git",
        "url": "https://github.com/PranamBhat/npm-package"
    },
    "author": "Pranam Bhat",
    "license": "MIT",
    "private": false,
    "engines": {
        "node": ">=6.0.0"
    },
    "engineStrict": true
}

Note: Make sure your package name is unique and not used by anyone over npm!

README.md

# @pranambhat/space
Removes space from the given string.
## Installation
```
$ npm install @pranambhat/space
```
## From Developer
Follow me on GitHub to stay updated about my latest projects: [![GitHub Follow](https://img.shields.io/badge/Connect-Pranam%20Bhat-blue.svg?logo=Github&longCache=true&style=social&label=Follow)](https://github.com/PranamBhat)
### Contact
For any queries : [email protected]

Note: To update your package and README.md with amazing badge’s, refer - https://shields.io/

Step 4

Now, it’s time to push all these changes to the GitHub repository. After editing these files, perform these commands,

git remote add origin your-github-repository
git init
git add .
git commit -m “initial commit for the npm package”
git push origin master —force

Step 5

Now, let’s publish our package. Execute these commands,

npm login

{It will ask your npm credentials. Also, your email address and two-factor authentication (2FA) OTP sent to your email address. Provide them carefully.}

npm publish

How to create and publish an npm package

That’s all!! Your package is now live!!!

You will get an email from npm right away!

How to create and publish an npm package

Step 6

Visit https://www.npmjs.com/ and go to your account > Packages.

How to create and publish an npm package

You will see your npm package. Next time, when you want to change something on the code, update "version": "2.0.0", and execute npm publish

How to create and publish an npm package

Conclusion

In this article, we have seen how we can create and publish an npm package. Hope this helps someone out there! Do share it with your friends and connections and let me know if you have any trouble while publishing your npm package!


Similar Articles