We have seen that the main command to install packages in NPM is npm install
. If we run it alone, it will install the dependencies listed in the package.json
file.
But we can also use npm install
to add new packages to our project. Installing packages is the most common way to use NPM.
Through this command, we can install packages in two ways:
- Locally, in the project folder, or globally, throughout the system.
In this article, we will see the local installation of packages
We will see the global installation of packages in How to install global packages
Local package installation
When installing a package locally, it is stored in the node_modules
folder of our project, and it will only be available for that particular project.
To install a package locally, we simply navigate to the root folder of the project and run this command,
npm install package-name
The package will be automatically downloaded from the NPM registry, saved in the node_modules
folder, and added to the package.json
file.
Package installation example
Let’s say we are working on a Node.js project and want to use the express
package, a popular library. To install it locally, we would run the following command:
npm install express
This will download the express
package from the NPM registry and save it in the node_modules
folder of our project.
This is the most common way of installation, because it prevents conflicts between package versions used in different projects