Post Reads 66 reads

Most of us may aware of the hosting packages on the NPM repository. So, In this article, we are going to learn about how to create and release NPM package on https://www.npmjs.com/

I have created and host the package https://www.npmjs.com/package/hello-world-by-mahesh

In this article, I’m going to share you how I have created this package and host it on the https://www.npmjs.com/

Without time waste lets try it step by step:

Create a Directory Create a Directory

NOTE:

I’m creating the package called hello-world-by-mahesh because of the package name should be unique.
For that, I am creating the NPM package called hello-world-by-mahesh and creating the GitHub repository called npm-hello-world. So, Don’t get confused.

Create a directory npm-hello-world where you want, I’m creating it in D:\xampp\htdocs\npm\

So, Open a terminal and navigate to the directory D:\xampp\htdocs\npm\ and creating the npm-hello-world with below command:

mkdir npm-hello-world

Then navigate inside the directory with:

cd npm-hello-world

Initialize Package Initialize Package

Now, Type command npm init to initialize the NPM package.

Add the information which comes on the terminal screen. It something looks like below:

D:\xampp\htdocs\npm\npm-hello-world
λ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (hello-world-by-mahesh)
version: (1.0.0)
description: Hello World
entry point: (index.js)
test command:
git repository: https://github.com/maheshwaghmare/npm-hello-world
keywords: npm-hello-world
license: (MIT)
About to write to D:\xampp\htdocs\npm\npm-hello-world\package.json:

{
  "name": "hello-world-by-mahesh",
  "version": "1.0.0",
  "description": "Hello World",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" &amp;&amp; exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/maheshwaghmare/npm-hello-world.git"
  },
  "keywords": [
    "npm-hello-world"
  ],
  "author": "Mahesh Waghmare",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/maheshwaghmare/npm-hello-world/issues"
  },
  "homepage": "https://github.com/maheshwaghmare/npm-hello-world#readme"
}


Is this OK? (yes) yes

NOTE: Here, I have created the GitHub repository npm-hello-world but, I have set the package name as hello-world-by-mahesh. Because npm-hello-world is not unique and which is exists (https://www.npmjs.com/package/npm-hello-world).

Login to NPM Login to NPM

Now, Use command npm login to login to your NPM account. e.g.

D:\xampp\htdocs\npm\npm-hello-world  (npm-hello-world@1.0.0)
λ npm login
Username: maheshwaghmare
Password:
Email: (this IS public) mwaghmare7@gmail.com
Logged in as maheshwaghmare on https://registry.npmjs.org/.

Okay. If you don’t have the NPM account then crate it at https://www.npmjs.com/ If you have already login.

Or

Do you want to see which account is currently logged in with?

Then use command npm whoami to see currently login username. E.g.

D:\xampp\htdocs\npm\npm-hello-world  (npm-hello-world@1.0.0)
λ npm whoami
maheshwaghmare

Sample Code Sample Code

In our sample package, we are creating the package which performs some mathematical operations.

These are addition, subtraction, multiplication and division.

Create a file index.js file and add below code into it:

/**
 * Addition of two numbers.
 */
module.exports.add = (a, b) => a + b

/**
 * Subtraction of two numbers.
 */
module.exports.sub = (a, b) => a - b

/**
 * Multiplication of two numbers.
 */
module.exports.mul = (a, b) => a * b

/**
 * Dividation of two numbers.
 */
module.exports.div = (a, b) => a / b

Here, We have created the simple arrow functions add, sub, mul & div and export them with module.exports to use it in another project.

We’ll see how to use it in the below section. Before it let’s publish our NPM package.

Publish the Package Publish the Package

Use command npm publish to publish your package. You can see something like below:

D:\xampp\htdocs\npm\npm-hello-world (master) (hello-world-by-mahesh@1.0.0)
λ npm publish
npm notice
npm notice package: hello-world-by-mahesh@1.0.0
npm notice === Tarball Contents ===
npm notice 305B index.js
npm notice 569B package.json
npm notice 133B README.md
npm notice === Tarball Details ===
npm notice name:          hello-world-by-mahesh
npm notice version:       1.0.0
npm notice package size:  592 B
npm notice unpacked size: 1.0 kB
npm notice shasum:        7ef94b6b51f33fe3228729c8f47b707cefe5ffca
npm notice integrity:     sha512-WRH3ux869bUAP[...]wRWMINQQNSXuA==
npm notice total files:   3
npm notice
+ hello-world-by-mahesh@1.0.0

You can see your published package at URL:
https://www.npmjs.com/package/{package-name}

My package hosted on URL:
https://www.npmjs.com/package/hello-world-by-mahesh

How to use it? How to use it?

We have hosted our package https://www.npmjs.com/package/hello-world-by-mahesh on NPM repository which performs the mathematical operations.

Now, We are going to create another project use-package-hello-world-by-mahesh to use our package hello-world-by-mahesh within it.

I have kept the suffix hello-world-by-mahesh to avoid the confusion.

Let’s get started.

Create and navigate to the directory use-package-hello-world-by-mahesh with commands:

  • mkdir use-package-hello-world-by-mahesh
  • cd use-package-hello-world-by-mahesh

E.g.

D:\xampp\htdocs\npm
λ mkdir use-package-hello-world-by-mahesh

D:\xampp\htdocs\npm
λ cd use-package-hello-world-by-mahesh 

Now, Initialize the NPM with npm init e.g.

D:\xampp\htdocs\npm\use-package-hello-world-by-mahesh
λ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (use-package-hello-world-by-mahesh)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
license: (MIT)
About to write to D:\xampp\htdocs\npm\use-package-hello-world-by-mahesh\package.json:

{
  "name": "use-package-hello-world-by-mahesh",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" &amp;&amp; exit 1"
  },
  "author": "Mahesh Waghmare",
  "license": "MIT"
}


Is this OK? (yes)

Now, Install our package hello-world-by-mahesh with command npm i hello-world-by-mahesh e.g.

D:\xampp\htdocs\npm\use-package-hello-world-by-mahesh  (use-package-hello-world-by-mahesh@1.0.0)
λ npm i hello-world-by-mahesh
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN use-package-hello-world-by-mahesh@1.0.0 No description
npm WARN use-package-hello-world-by-mahesh@1.0.0 No repository field.

+ hello-world-by-mahesh@1.0.1
added 1 package from 1 contributor and audited 1 package in 3.046s

Create a index.js file and add below code:

const {add, sub, mul, div} = require('hello-world-by-mahesh')

console.log( add( 10, 20 ) );
console.log( sub( 10, 20 ) );
console.log( mul( 10, 20 ) );
console.log( div( 10, 20 ) );

Here,

  • const {add, sub, mul, div} = require(‘hello-world-by-mahesh’) we have extracted the methods add, sub, mul, div from our package with require('hello-world-by-mahesh').
  • add( 10, 20 ) perform the addition operation from our package hello-world-by-mahesh.
  • sub( 10, 20 ) perform the addition operation from our package hello-world-by-mahesh.
  • mul( 10, 20 ) perform the addition operation from our package hello-world-by-mahesh.
  • div( 10, 20 ) perform the addition operation from our package hello-world-by-mahesh.

Lets, Execute our index.js file with the command node index.js. Eg.

D:\xampp\htdocs\npm\use-package-hello-world-by-mahesh  (use-package-hello-world-by-mahesh@1.0.0)
λ node index.js
30
-10
200
0.5

Here, We see that our package works successfully as expected.

Conclusion Conclusion

In this article, we have created a simple NPM package and host it on NPM packages directory at https://www.npmjs.com/. You can also try to create some packages which may useful for anyone.

How to Create and Release NPM Package.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top
%d bloggers like this: