DEV Community

Cover image for How to make a little npm package and publish it
Dhairya Shah
Dhairya Shah

Posted on • Edited on • Originally published at codewithsnowbit.hashnode.dev

How to make a little npm package and publish it

Really! It is very easy...

npm stands for node package manager

In Breif

Every npm require one package.json with name and version properties

{
   "name": "string-seperator",
   "version": "1.0.2"
}
Enter fullscreen mode Exit fullscreen mode

Step 1 - npm account

the most important thing, just go and sign up

Step 2 - login to npm via CLI

I guess you have made an account there

So, go to your terminal and type

$ npm login
Enter fullscreen mode Exit fullscreen mode

Step 3 - Initialize npm

You need to initialize your project in order to publish the package to the npm registry

To initialize your project, run the following command

$ npm init
Enter fullscreen mode Exit fullscreen mode

It will look like this

{
  "name": "PROJECT_NAME",
  "version": "1.0.0",
  "description": "PROJECT_DESCRIPTION",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "AUTHOR_NAME",
  "license": "ISC"
}
Enter fullscreen mode Exit fullscreen mode

Let's code

  • Go to your root repository and create index.js
  • Now write the following code in your index.js
function greet(string){
    return "Hello, " + string;
}

module.exports = greet;
Enter fullscreen mode Exit fullscreen mode
  • tada, now you have completed the core part of the project πŸŽ‰

Publishing the package to the npm registry

  • Make sure to check all your code before publishing
  • Now head up to terminal again πŸ‘¨πŸ»β€πŸ’»
$ npm publish --access public
Enter fullscreen mode Exit fullscreen mode

Here --access public is a scope that tells npm to publish your package to the npm registry publicly.

πŸŽ‰

You made it, you can check out your published package from ↓
Check out your package at npm registry


Make sure to check out: https://snowbit.bio.link/

Make sure to check out my YouTube Channel: https://www.youtube.com/channel/UCNTKqF1vhFYX_v0ERnUa1RQ

Top comments (1)

Collapse
 
sonnenhohl profile image
Artur Sonnenhohl

Interesting, really super easy!