We have seen that the main command to install packages in NPM is npm install
. If we execute 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.
Local installation of packages
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.
This is the most common form of installation, because it avoids conflicts between package versions used in different projects.
To install a package locally, we simply navigate to the root folder of the project, and execute 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.
Example of package installation
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 execute 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.