DEV Community

Cover image for How to Publish your awesome package to NPM 👏
Shahjada Talukdar for The Destro Dev Show

Posted on

How to Publish your awesome package to NPM 👏

For JavaScript developers, npmjs.com is a treasure!

You can find more than 1 Million packages there. ( Yeah, I agree all of them are not useful 😉 )

We can install/download Packages like react, express, lodash etc into our projects very easily from the npmjs package repository with just one command.
Such as -

npm i react
Enter fullscreen mode Exit fullscreen mode

Many a time, we want to reuse our code to different artifacts. And it's a very good practice in Software Engineering.

What we do a lot, we create an independent repo/project for the code we want to reuse and copy that project folder into a new application that does not scale well. 😞

To solve this issue, we can upload/publish our own code/package to npmjs.

And then we can download/install it to a new app and just import it where we need to use it.

This is also a great way to modularize your app.

For initializing npm, we can run this command in our project directly-

npm init
Enter fullscreen mode Exit fullscreen mode

Now we can code our awesome Library and test it well before publishing to npmjs.

You should give a proper name and version in your package.json file

{
  "name": "my-awesome-package",
  "version": "1.0.0",
}
Enter fullscreen mode Exit fullscreen mode

To publish, you need one npmjs account, you can create it here https://www.npmjs.com/signup .

Then you can login in your terminal-

npm login
Enter fullscreen mode Exit fullscreen mode

You can put your credentials and you will be logged in!

Now, let's do the final Publish...

npm publish
Enter fullscreen mode Exit fullscreen mode

Awesome!
Now if you have a look on your npmjs package page after loggin in on your browser, you can see your package is listed there.
Normally you should see it here https://www.npmjs.com/settings/{UserID}/packages

Publish your reusable code and have fun!

Cheers!

👋

As I am trying to contribute contents on the Web, you can buy me a coffee for my hours spent on all of these ❤️😊🌸
Buy Me A Coffee

Top comments (4)

Collapse
 
osdevisnot profile image
Abhishek Shende

While we are at it, I might as well point you to my recent work on NPM publishing.

klap is a a zero config, zero dependency bundler for tiny javascript packages.

dev.to/osdevisnot/show-dev-introdu...

Collapse
 
destro_mas profile image
Shahjada Talukdar

Thanks for sharing 👍
Will have a look!

Collapse
 
daggron profile image
Abhay Sharma

I have published my package .Thanks Sir

Collapse
 
destro_mas profile image
Shahjada Talukdar

Awesome! You are welcome!