The main function of NPM is to install and manage our project’s dependencies. To do this, as we know, it records the dependencies in the package.json file.
These dependencies are organized into two categories:
- Dependencies: Dependencies required for execution
- DevDependencies: Dependencies needed only during development
If we have our dependencies correctly configured in the package.json file, we can install them simply with the following command,
npm install
This command will read the package.json file and download all dependencies specified in the dependencies section. These dependencies will be stored in a folder called node_modules in the project directory.
This is very common when downloading an Open Source project. When publishing a project, it is saved in repositories (like Github) without the libraries. This is logical, to save space.
A very common mistake for beginners is to download an Open Source project and think it doesn’t work. Often, it’s simply that we have forgotten to install the dependencies with this command.
Installing Required Dependencies
When npm install is executed without any additional options, NPM will install the dependencies found under the dependencies section in the package.json file. These are the dependencies needed for the application to function correctly in a production environment.
Suppose we have a package.json file like the following,
{ “name”: “mi-proyecto”, “version”: “1.0.0”, “dependencies”: { “express”: “^4.17.1”, “lodash”: “^4.17.21” } }
In this case, npm install will download and install the latest versions of the express and lodash libraries.
Installing Development Dependencies
If we want to install the dependencies needed for project development (like unit tests, build tools, or code style checkers), we must use the --dev option or its short form -D. For example:
npm install —dev
This will install all dependencies found in the devDependencies section of our package.json file.
Remember that these dependencies will not be included in the final package when we distribute our application.
