Ensuring Development Tools: Installing devDependencies with npm in Node.js Projects
- By default,
npm install
prioritizes installing dependencies listed under thedependencies
section in yourpackage.json
file. These dependencies are considered essential for the project to function in both development and production environments. - The
devDependencies
section, on the other hand, contains dependencies that are only needed during development activities (testing, linting, code formatting, etc.).
When devDependencies
Aren't Installed:
There are two main reasons why npm install
might skip devDependencies
:
Production Mode:
--only=prod
Flag:
How to Install devDependencies
:
Here's how you can ensure devDependencies
are installed:
Basic
npm install
:npm install --include=dev
:
Example:
# Install both production and development dependencies
npm install
# Install only development dependencies
npm install --include=dev
Key Points:
- Understand the distinction between
dependencies
anddevDependencies
inpackage.json
. - Be aware of the
--production
flag andNODE_ENV
variable that can affect installation behavior. - Use
--include=dev
when you specifically needdevDependencies
.
# This will install both dependencies and devDependencies from package.json
npm install
Installing only devDependencies with --include=dev:
# This will install only devDependencies from package.json
npm install --include=dev
package.json with example dependencies and devDependencies:
{
"name": "my-project",
"version": "1.0.0",
"dependencies": {
"express": "^4.18.2" # Production dependency (example)
},
"devDependencies": {
"jest": "^28.0.1", # Development dependency (example)
"eslint": "^8.24.0" # Development dependency (example)
}
}
Explanation:
- In the first code, we simply run
npm install
. This assumes you're not in production mode (no--production
flag orNODE_ENV
set toproduction
) and will install everything listed inpackage.json
, including bothdependencies
anddevDependencies
. - The second code uses the
--include=dev
flag to explicitly tellnpm
to install only development dependencies, even if you're technically in production mode. This is useful if you need to set up a development environment on a production server. - The third code shows an example
package.json
file with a production dependency (express
) and two development dependencies (jest
andeslint
). These would be installed based on thenpm install
command used.
This method allows you to install a specific development dependency directly. Replace <package-name>
with the actual package you want to install. This will not only install the package but also add it to the devDependencies
section of your package.json
file, ensuring it's included in future installations.
Manual Installation:
In rare cases, you might need to install a package manually. Here's the general process:
- Place the downloaded file in your project's
node_modules
directory. - You might need to manually install any additional dependencies for the downloaded package.
However, it's generally recommended to use npm install
with flags for managing dependencies as it provides better control, simplifies tracking dependencies, and ensures compatibility.
Here's a table summarizing the methods:
Method | Description |
---|---|
npm install --include=dev | Installs all dependencies and devDependencies |
npm install <package-name> --save-dev | Installs a specific devDependency and adds it to package.json |
Manual Installation | Download and place the package manually in node_modules (not recommended) |
node.js npm