What is npm?

This is similar to cargo for Rust, pip for Python, vcpkg for C++, gem for Ruby, composer for PHP, etc,
npm is default package manager for JavaScript & JavaScript runtime environment Node.js.
It is an online database of public and paid-for private packages, called the npm registry. The registry is accessed via the client, and the available packages can be browsed and searched via the npm website.

Installation on WSL


curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash
source ~/.bashrc
nvm install --lts
nvm use --lts
      

Example Usage

Hello, World Example

Create package.json

$ mkdir test
$ cd test

// You can create a package.json file manually or
// by running the following command (npm init)
$ cat package.json
{
  "name": "hello-world",
  "version": "1.0.0",
  "scripts": {
    "test": "node test.js"
  }
}

// Install pacakges in package.json
$ npm install
up to date, audited 1 package in 156ms

$ npm list
hello-world@1.0.0 /home/amit/hello-world
└── (empty)
              
Create index.js and test.js(module to test index.js)

// Create index.js
$ cat index.js
function hello() {
  return "Hello, world!";
}
module.exports = hello;

// Create test.js
$ cat test.js
const assert = require("assert");
const hello = require("./index");

assert.strictEqual(hello(), "Hello, world!");
console.log("tests passed");
              
Run test

$ npm test

> hello-world@1.0.0 test
> node test.js

tests passed