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, across the entire 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 saved in the node_modules folder of our project, and will only be available for that particular project.
To install a package locally, we simply navigate to the project’s root folder and execute this command:
npm install nombre-del-paquete
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
Suppose 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 form of installation, because it prevents us from having conflicts between package versions used in different projects.
