DEV Community

saurabh satapathy
saurabh satapathy

Posted on • Edited on

An Easy Guide to Publishing npm packages

Anyone who has worked in JavaScript frameworks knows what a npm package is? And how important it is? So, what's your thought on publishing your own package? Ya, I know it is cool to install your own package using npm i your_component_name 🤩🤩. All these things are very meaningful to a beginner. It will definitely bring a smile on your face 😇.

Here, We are going to see some easy steps through which you can publish your npm package. For this first you have to build a component that you want to publish. After successfully creating a component just follow the below steps.

  • Keep all required files of your component in a single folder let assume it is named as component. component folder inside a folder (⚠️All required files of your component must be inside the component folder)
  • Now create a index.js file inside this. Refer below image 👇

creating index.js

  • Now, just import and export your component in index.js file like this 👇
import your_component_name from './component/your_component_name'
export default your_component_name;
Enter fullscreen mode Exit fullscreen mode

(Or, just open this folder with vs code, create a new file named index.js, write these two lines and save it.)
⚠️Replace your_component_name with your component name.

  • If you don't have an npm account just goto npm on your browser and sign up. click here to sign up with npm

  • Open cmd with in the same folder. Easy way just write cmd at place of your folders path and click enter. Refer below 👇

opening cmd with in the same folder

  • Login to npm by this command npm add user
    (It will ask your usename, password and email just give and log your self in)

  • Now run this command npm init
    It will ask package name, version, description, entry point, test command, git repository (if your component is in your git then give the repository link), keywords (these keywords are helpful for showing your package name in npm search), author (give like this your_name < your email id > ) and license ("MIT" or "ISC" which you like), then write yes and click enter.

  • After the above you can see your package.json created over there.

  • If you have used any third party library then just install them here by which all of these will listed as dependency in your package.json which will help user to get everything installed by a single npm install.

  • Now, create a readme.md describing your component which will be shown in npm.

  • Go back to cmd and run npm publish

  • Now, you can see your package listed in npm.

🎉🎉🎉🎉🎉
Congratulations, you have successfully published your npm package.

Link to my npm packages

Thank you

Ways to reach me:

Top comments (3)

Collapse
 
jankapunkt profile image
Jan Küster

Please also consider the following important tools:

  • npm publish --dry-run for testing if everything will be published.
  • enable 2FA with TOTP for publishing to help reduce supply chain attacks
  • prepublish hook to run tests, lint and audit before publish

Let's all make the ecosystem more safe.for everyone 🤝

Collapse
 
andrewbaisden profile image
Andrew Baisden

Nicely done tutorial.

Collapse
 
saurabh190802 profile image
saurabh satapathy

Thanks sir