NPM can install all the dependencies of a project through the package.json file. It can also update and uninstall packages. In the package.json file, each dependency can specify a range of valid versions using the semantic versioning scheme, allowing developers to auto-update their packages while at the same time avoiding unwanted breaking changes.
Some facts about NPM:
- At the time of writing this article, NPM has 580096 registered packages. The average rate of growth of this number is 291/day which outraces every other package registry.
- npm is open source
- The top npm packages in the decreasing order are: lodash, async, react, request, express.
To install NPM, it is required to install Node.js as NPM gets installed with Node.js automatically.
Checking and updating npm version:
Version of npm installed on system can be checked using following syntax:
If the installed version is not latest, one can always update it using the given syntax:
npm npm@latest -g.
As npm is a global package, -g flag is used to update it globally.
Creating a Node Project:
To create a Node project, npm init is used in the folder in which user want to create project. The npm command line will ask a number of questions like name, license, scripts, description, author, keywords, version, main file etc. After npm is done creating the project, a package.json file will be visible in project folder as a proof that the project has been initialized.
After creating the project, next step is to incorporate the packages and modules to be used in the Node Project. To install packages and modules in the project use the following syntax:
npm install package_name
Example: Installing the express package into the project. Express is the web development framework used by the Node.
npm install express
To use express in the Node, follow the below syntax:
var express = require('express');
Example: To install a package globally (accessible by all projects in system), add an extra -g tag in syntax used to install the package.
Installing nodemon package globally.
npm install nodemon -g
Controlling where the package gets installed:
To install a package and simultaneously save it in package.json file (in case using Node.js), add –save flag. The –save flag is default in npm install command so it is equal to npm install package_name command.
npm install express --save
By –save flag one can control where the packages are to be installed.
–save-prod : Using this packages will appear in Dependencies which is also by default.
–save-dev : Using this packages will get appear in devDependencies and will only be used in the development mode.
Example: npm install node-color –save-dev
If there is a package.json file with all the packages mentioned as dependencies already, just type npm install in terminal. npm will look at package.json file and install all the dependencies according to their mentioned versions. This command is typically used when a Node project is forked and cloned. The node_modules being a big folder is generally not pushed to a github repo and the cloner has to run npm install to install the dependencies.
Note: NPM installs the dependencies in local mode (Default) which go to the node_modules directory present in the folder of Node application. To see all the locally installed modules use npm ls command.
To uninstall packages using npm, follow the below syntax:
Example: To uninstall the express package
To uninstall global packages, follow the below syntax:
npm uninstall package_name -g
Using Semantic Versioning to manage packages:
- To install a package of a specific version, mention the full and exact version in the package.json file.
- To install the latest version of the package, mention “*” in front of the dependency or “latest”. This will find the latest stable version of the module and install it.
- To install any version (stable one) above a given version, mention it like in the example below:
“express”:”^4.1.1″. in package.json file. The caret symbol (^) is used to tell the npm to find a version greater than 4.1.1 and install it.
- Nodejs | Automatic restart NodeJs server with nodemon
- How to install the previous version of node.js and npm ?
- Node.js NPM arraybuffer-to-string Module
- Node.js NPM string-to-arraybuffer Module
- Node.js NPM shortid Module
- Node.js NPM uuid
- How to install modules without npm in node.js ?
- Responsibilities of a File Manager
- Context Manager Using @contextmanager Decorator
- What are the differences between npm and npx ?
- Steps to Create and Publish NPM packages
- How to use External Modules and NPM in a project ?
- Difference between npm and yarn
- ReactJS | Basics of ES, Babel and npm
- How to import SASS through npm ?
- How to update NPM ?
- How to import LESS through npm ?
- Introduction to NPM scripts
- What is the meaning of --save for NPM install ?
- Node.js | package.json
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.