How to make a little npm package and publish it

How to make a little npm package and publish it

ยท

2 min read

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"
}

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

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

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"
}

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;
  • 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
    
    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: snowbit.bio.link

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

ย