What is NPM?

NPM is Node Package Manager, the default package manager of Node JS.

NPM is open source and worlds largest Software library used by 11,000,000+ (11M) JavaScript developers and has 2.1 Millions Packages . NPM always comes with Node JS. To check whether NPM is installed with node in your system, type npm in terminal. To check npm version, type npm --version.

10.2.4
npm --version
10.2.4
npm -v

NPM is used to download Node JS Modules from online registry or library of NPM, i.e. https://www.npmjs.com//. We can download node js modules and check online documentation on NPM site.

We can also upload your code, i.e. module on https://www.npmjs.com/.

  1. NPM is purchased by Github.
  2. Public registries will remain free and available forever.

npm init

To start building any project using Node JS, first we have to build a package.json file. To build your package.json file for nodeapp folder, just open your nodeapp project in VS Code. Now Open terminal in VS Code and type npm init. See example below

This utility will walk you through creating a package.json file.

It only covers the most common items, and tries to guess sensible defaults.


See `npm help json` for definitive documentation on these fields and exactly what they do.


Use `npm install <pkg>` afterwards to install a package and save it as a dependency in the package.json file.


Press ^C at any time to quit.

package name: (nodeapp)

npm init

Create package.json file

Type npm init and follow these steps to build your package.json file using NPM.

  1. Enter Package Name → Name of app
  2. Version. → (Default is 1.0.0)
  3. description → description of app
  4. entry point → test.js
  5. test command → command for testing
  6. git repository →
  7. keywords →
  8. author → Avinash Malhotra
  9. license →
  10. Is this OK? → press enter

Now you can see a package.json in your root directory.

package.json file code


{
  "name": "nodeapp",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Avinash Malhotra",
  "license": "ISC"
}    

To install package.json without mentioning name, version etc, simply type "npm init --yes".


npm start

To start your app, we need to type node test.js in VS Code terminal every time.

Lets move to the next step. Create a new directory (folder) in nodeapp folder named "src". Move test.js file from root directory to src directory. Don't move package.json from root directory. See screen shot below.

node js directory structure
node js directory structure


Now to start your project, type node src/test.js. But instead of this, we can build a custom command for npm to start our project. To do this, open package.json and add following code in script property.

Add start property in scripts of package.json


{
  "name": "nodeapp",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start":"node src/app.js"   
  },
  "author": "Avinash Malhotra",
  "license": "ISC"
}    

To start your project now, earlier we use node src/test.js. But now just type npm start.

hello node

npm start

npm install

npm install is used to add modules from npmjs.org.

Lets say, we want to express.js in our project, first we need to download express.js from npmjs.org and then only we can use it.

npm install

npm install express

npm install --save-dev

To install developer dependencies.

npm install --save-dev nodemon

npm i

Shortcut of install

npm i express

install multiple modules

npm i express body-parser nunjucks

uninstall module

npm uninstall colors

NPM 5.0 and above don't require --save option to add modules in your dependencies available in package.json file. --save option was required only for npm versions below 5.

package.json after installing express


{
  "name": "nodeapp",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Avinash Malhotra",
  "license": "ISC",
  "dependencies": {
   "express": "^4.16.4",
    }
}    

In the same way, we can install other modules from npmjs.org. by using npm install dependency or npm i dependency .


npm install

npm install is used to add all dependencies available in package.json files into node_modules folder. Lets say, we want to share our project to someone through git or file sharing, we can remove, or gitignore node_modules folder while transferring app. After downloading the app, we have to type npm install to refresh our node_modules folder. All the dependencies will be automatically downloaded.

To refresh modules from package.json file, use npm install. This will install all dependencies listed in node_modules folder.

npm install

npm uninstall

npm uninstall command is used to remove dependency from node_modules and then update package.json file.

removed colors

npm uninstall colors