First of all, NPM stands for Node Package Manager. It is also known as Ninja Pumpkin Mutants or Non-Profit Pizza Makers. Npm is a package manager for Javascript Programming Language. It is a package manager for NodeJs packages or modules if you like. It is the default package manager for the Javascript runtime environment NodeJs. The NPM is installed on your system when you install nodejs. www.npmjs.com hosts thousands of free packages to download and use.
NPM provides two main functionalities:
- Online repositories for node.js packages/modules which are searchable on search.nodejs.org.
- Command line utility to install Node.js packages, do version management and dependency management of Node.js packages.
Installing modules using NPM:
$npm install <module name>
Example, following is the command to install a famous node.js framework module call express:
$npm install express
or
$npm i express
Now, you can use this module in your js file as:
const express = require('express');
What is package and package.json?
Simply, a package in node.js contains all the files you need for a module where modules are javascript libraries you can include in your project.
The package.json file is the heart of node.js system. It is the manifest file of any Node.js project and contains the metadata of the project. The package.json file is the essential part of understand, learn and work with the node.js. It is the first step to learn about development in Node.js.
What does package.json file consist of?
The metadata information in package.json file can be categorized into below categories:
Identifying metadata properties:
It basically consists of the properties to identify the module/project such as the name of the project , current version of the module, license, author of the project, description about the project,etc.
Functional metadata properties:
As the name suggests, it consists of the functional values/properties of the project/module such as the entry/starting point of the module/dependencies in project, scripts being used, repository links of node project,etc.
Example of package.json file is as follow:
{
"name": "website",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "yourName",
"license": "ISC",
"dependencies": {
"mongodb": "^4.3.1",
"@nestjs/common": "^9.0.0",
},
"devDependencies": {
"@nestjs/cli": "^9.0.0",
"@nestjs/schematics": "^9.0.0",
"@nestjs/testing": "^9.0.0"
}
}
The main purpose of package.json is to maintain the dependencies among projects.
Installing a package as production dependencies:
$npm install moment
Installing a package as development dependencies:
$npm install express --save-dev
Install a package globally:
$npm i body-parser -g
Uninstall locally installed packages:
$npm uninstall moment //for production dependency
$npm uninstall express --save-dev //for development dependency
Uninstalling packages globally:
$npm uninstall body-parser -g
Listing installed packages:
$npm list //lists local packages
$npm list -g //for global packages
//Displays child dependencies too.