DEV Community

Cover image for Write and Publish your first NPM package
Oshadha Shiro
Oshadha Shiro

Posted on

Write and Publish your first NPM package

Hello Guys I am Shiro and Today I'm going to tell you how to write and publish your first NPM package..!

Prerequisites

  1. NodeJS & NPM
  2. account on npmjs.com
  3. Visual Studio Code

Login to NPM

$ npm login

Login

Fill these details and login.

Use this command to know if you have successfully logged in.

$ npm whoami

Whoami

If you can see your username you have successfully logged in

Create your NPM package

First create a new folder to hold your package. then open that folder in terminal and use this command.

$ npm init -y

Write your code

Then open the folder in vscode and create a file called index.js. In here I am going to show you how to write a simple package to multiply two numbers.

You can copy and paste this code into index.js

NPM_PACKAGE_CODE

Publish package

You can use this command to publish your first NPM package

$ npm publish

If there is no error you can goto your Profile in NPM and see your first NPM package.

Install your package

You can install your package and test it by using this command

$ npm install <package name>

Congratulation..! 🎉 You have successfully published your first NPM package :)

If you have any problem comment below

Happy Coding 🚀

Latest comments (0)