Hello everyone, we are going to learn about Node Package Manager (NPM) in this lesson. We talked slightly about it in Lesson 1 and initialized the project. Let’s dive deep now.

What is NPM

As the name suggests, it’s a Package Manager for Node. Open Source Developers from all around the globe can publish their Node packages and use the packages published by other people for free.

It is an online repository for the publishing of open-source Nodejs projects.

It is also a command-line utility to interact with packages in a Nodejs project and manage dependencies. The dependencies are just the packages that are being used in the project. npm cli helps easily install and remove the packages available on the npm registry.

It helps us rapidly build software with the help of the 1,372k+ packages present on the npm registry. If you’re stuck with a problem, chances are there is already a solution contributed by someone which you can directly.

NPM comes with standard Node installation. We already have a cli to interact with npm packages. You can check by running npm –version in the terminal. Now let’s understand how it works and how can we create a package ourselves.

How to create a module and share it with community

Suppose you have created a package and you want to share it on npm as a package. To keep things basic, let’s consider this is your awesome package:

// index.js
module.exports = function(){
    console.log("My awesome module at so share");
}

To share this package on NPM, you will have to initialize the repository as an npm package. To do that, you simply have to run one command:

npm init

The cli will ask you a bunch of questions regarding package name, version, author, etc and generate a package.json file. It is the same file that is used to track the package version too.

{
  "name": "supermodule",
  "version": "1.0.0",
  "description": "ERROR: No README data found!",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Your name here",
  "license": "ISC",
  "repository": ""
}

Now we are ready to publish your awesome package.

#node #npm #javascript #web-development #programming

Node.js Lesson 3: Node Package Manager
1.95 GEEK