![]() |
VOOZH | about |
NPM is like a powerhouse for Node.js that contains all the necessary modules for the smooth running of the node.js application. It gets installed on our machine when we install Node.js on our Windows, Linux or MAC OS.
How to install Node on the machine? Refer to article.
NPM has 580096 registered packages. The average rate of growth of this number is 291/day that means the growth of different kind of packages increasing drastically, so we have to update our node every time on our machine ? The answer is No! NPM allows us to install third-party modules on our machine according to the need of our work.
Another reason is predefined modules cannot be able to fulfill the need for big projects for e.g. HTTP modules cannot differentiate the multiple kinds of requests, so we have to install externally another popular module. i.e express module.
We can access third party modules using some predefined commands provided by Node Package Manager are given below:
Initial Project Structure :
👁 Imagenpm install command: This npm command is used for installing the third party modules in our current directory. There are two different ways to use this command:
Package.json: create a package.json file in the directory and mention express dependencies in this file.
{
"name": "gfg",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
}
}
Run npm install command:
npm i or npm install👁 Image
Updated Project Structure:
package-lock.json file and node_modules created package-lock.json file contains all the necessary information of downloaded extra dependencies and node_modules folder contains all the different types of packages that installed along with our specified module in the package.json.
👁 ImageParameter: Parameter can be the name of the module that we want to install or folder name in which we want to install all third-party modules in the directory. By default, the folder is node_modules that contains all the installed modules. This folder is automatically generated when we install any external module the first time.
Syntax:
npm install [-g] [<package>..]
- Syntax for installing module: Module will get install in the node_modules folder in the present directory.
npm install <module-name>
- Syntax for installing any module globally: Globally installing means we can access the module without installing that module in a particular directory. For eg Nodemon module etc.
npm install -g <module-name>
- Syntax for change the directory path of modules: This command will change the installing path of the external modules from node_modules to <dirname> folder in the working directory.
npm install <dirname>
Explanation: After installing any new module new packages are added to the node_modules folder and dependencies are updated to the package.json file.
npm install mongodb👁 Image
package.json file:
👁 Imagenpm update command: This npm command is used for updating the dependencies that are mention in the package.json file as well as install all the missing packages in the directory and also used for updating the current node version on the machine.This command used in two different ways:
Syntax:
npm update -g
Updating nodemon module that was installed globally:
👁 ImageThere are mainly types of dependencies used in Node.js:
1. Caret Dependencies: When dependencies present in the package.json or package.lock.json file with ^ called Caret Symbol is called Caret Dependencies.These dependencies are updated to the latest version compatible to that version.
"dependencies": {
"dep11": "^2.2.2"
}
This npm update command will update to 2.3.3 (consider this version exists) and 2.3.3 satisfy previous version
2.Tilde Dependencies: npm update command will update these dependencies to the highest sorted version. These dependencies use ~ symbol.
"dependencies": {
"dep11": "^2.2.2"
}
If we update this dependency, it will get updated to 2.2.3 version in this case.
Difference: