How to create a NPM package 💭

What is npm?

NPM(Node Package Manager) makes it easy for JavaScript developers to share the code that they’ve created to solve particular problems, and for other developers to reuse that code in their own applications.

Installing npm

Initially check does node is installed in your computer,

node -v

its returns the version of the installed package .

If its displays any error message install the latest version of the nodejs.

Check the version of the npm using

npm -v

make sure your npm version is up-to date.Use the below command to install the latest npm.

npm install npm@latest -g

Configure NPM

Before creating a package,We need to set some common properties that are gonna replicated in all packages.

npm set init-author-name “authorname”
npm set init-author-email “email id”
npm set init-author-url “author page”

if author’s page is not available skip it.

npm set init-license “license type”
cat ~/.npmrc

Save the property using save-exact property

npm set save-exact true

add new user using

npm adduser

enter the username and email id.

if you don’t have the npm account just create a new one in NPM.

Create package

Create a New Folder for the package

Run “npm init” to initialize properties for your package.It will create package.json inside your folder.

Type package name,version,entry point(index.js),description,test command,git repo,keywords,license details.

open the package.json file

Create entry point(index.js)

After adding all the dependencies,publish the package using

npm publish

Additional to that add the file before publishing the package.The content in the read me file will be displayed in the NPM package page for the users to understand the package.

If there are any updates after the package is published.You can version your package, If its

Patch Release

eg. 1.0.0 → 1.0.1

npm version patch

Minor Release

eg. 1.0.0 → 1.1.0

npm version minor

Major Release

eg. 1.0.0 → 2.0.0

npm version major
Semantic Versioning

Then publish the package using,

npm publish

That’s it !! Now you are owning a package in NPM :)

Happy publishing :)