Stats: There are more than 1.7 million packages in npm’s archive/repo/directory (whatever you say it).
Now here’s the question how you can add your package to this vast collection? and might help a bunch of devs in something they’re having hard time doing it, or it’s just for fun ¯\_(ツ)_/¯.
Follow my lead and we’ll publish a package in no time (⌐■_■).
Things you need
- JavaScript ( of course to create a package )
- Node.js (That’s how you’ll create a package)
- NPM (Duh!!)
- An account on npm’s official site ( here )
- And access to a terminal ( to publish it. )
Note: You don’t have to be a pro in JS, just basics will suffice.
Part 1: (Preparation)
First we’ll install and prep some before we create a package and publish it.
Step 1:
Install Node.js from it’s official site (here!!)
Step 2:
Create an account on npmJs official site (here!!)
Step 3:
Initialize an npm project folder. Follow along.
- Create a folder and open terminal into it or use the following commands in sequence.
Commands:
- Create a folder.
$~ mkdir your_package_name
- cd into it.
$~ cd your_package_name
Now folder is ready, we want to initialize it as an npm project.
- Run the following command
$~ npm init
It’ll ask for some values enter as per your package with reference to what I’ve explained here.
Focus on the comments after every property. This is how your package.json file will look like.
Note: If you create a github repo first and then npm init
, then it’ll by default contain your git repo’s link.
Part 2: (Publishing)
Now we’ll publish a simple npm package that I create few days back. This package returns a number based on the character/letter/string you provide to it. Have a look here. (❁´◡`❁).
Back to publishing the code.
Once you’ve coded your package with main export in index.js
follow along.
Step 1:
Login via terminal into npm.
Enter the command and login by providing your username, password and the email you’ve made public on your npm’s profile.
Step 2:
Once you have completed the package (coding stuff.) then open terminal into your package’s folder and enter the following command.
$~ npm publish
That’s it !! ¯\_(ツ)_/¯. Congratulations you’ve published your first package, do share it down in the comment section.
Give some claps 👏 if liked it, follow if you love to read these kind of blogs and to support me. And subscribe if you like.
Signing off Soyokaze ❤.