We install packages to add features to our project. Some packages are needed when the project runs, others only when we build or test it.
Installing packages (dependencies vs devDependencies) in Node.js
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Node.js
npm install <package-name> # installs as a dependency npm install <package-name> --save-dev # installs as a devDependency
Dependencies are packages your app needs to work when running.
DevDependencies are packages only needed during development, like testing or building tools.
Examples
express package as a dependency because your app needs it to run.Node.js
npm install express
jest as a devDependency because it is only used for testing during development.Node.js
npm install jest --save-dev
lodash is installed as a dependency by default, useful for utility functions your app uses.Node.js
npm install lodash
eslint is a tool to check code style, so it is installed as a devDependency.Node.js
npm install eslint --save-dev
Sample Program
This package.json file shows express under dependencies because it is needed to run the app. jest is under devDependencies because it is only used for testing during development.
Node.js
/* package.json example showing dependencies and devDependencies */ { "name": "my-app", "version": "1.0.0", "dependencies": { "express": "^4.18.2" }, "devDependencies": { "jest": "^29.5.0" } }
Important Notes
Use npm install without flags to add runtime packages.
Use --save-dev to add packages only needed during development.
Keeping devDependencies separate helps keep production installs smaller and faster.
Summary
Dependencies are needed to run your app.
DevDependencies are only needed during development.
Use npm install and npm install --save-dev to manage them correctly.
Practice
1. Which command installs a package as a regular dependency needed to run your Node.js app?
easy
Solution
Step 1: Understand the difference between dependencies and devDependencies
Dependencies are packages needed when your app runs, while devDependencies are only needed during development.Step 2: Identify the correct npm command for dependencies
The commandnpm install expressinstalls the package as a regular dependency by default.Final Answer:
npm install express -> Option CQuick Check:
Regular dependencies usenpm installwithout flags [OK]
Hint: Use plain npm install for runtime dependencies [OK]
Common Mistakes:
- Using --save-dev for packages needed at runtime
- Confusing uninstall with install commands
- Assuming npm update installs packages
2. Which of the following commands correctly installs a package as a development dependency?
easy
Solution
Step 1: Recognize the flag for devDependencies
The flag--save-devtells npm to install the package as a development dependency.Step 2: Match the command with the correct flag
npm install --save-dev jestinstalls Jest as a devDependency, which is used for testing during development.Final Answer:
npm install --save-dev jest -> Option BQuick Check:
Dev dependencies use--save-devflag [OK]
Hint: Use --save-dev flag for development-only packages [OK]
Common Mistakes:
- Omitting --save-dev for dev dependencies
- Using --prod which is not a valid flag for devDependencies
- Installing globally instead of locally
3. Given the following
package.json snippet, which package will NOT be installed when running npm install --production?
{
"dependencies": {
"express": "^4.18.2"
},
"devDependencies": {
"mocha": "^10.2.0"
}
}medium
Solution
Step 1: Understand the effect of --production flag
The--productionflag tells npm to install only the packages listed underdependencies, skippingdevDependencies.Step 2: Identify which packages are devDependencies
Here,mochais underdevDependencies, so it will NOT be installed withnpm install --production.Final Answer:
mocha -> Option AQuick Check:
DevDependencies skipped with --production flag [OK]
Hint: npm install --production skips devDependencies [OK]
Common Mistakes:
- Assuming all packages install regardless of flags
- Confusing dependencies with devDependencies
- Thinking --production installs devDependencies
4. You run
npm install --save-dev eslint but later realize you need eslint as a regular dependency. What is the best way to fix this?medium
Solution
Step 1: Understand npm behavior with existing packages
Runningnpm install eslintalone will not move it from devDependencies to dependencies if already installed as devDependency.Step 2: Properly remove and reinstall as dependency
Uninstalling first removes it from devDependencies, then reinstalling adds it to dependencies.Final Answer:
Run npm uninstall eslint then npm install eslint -> Option DQuick Check:
Uninstall then install moves package between dependency types [OK]
Hint: Uninstall then reinstall to change dependency type [OK]
Common Mistakes:
- Trying to install without uninstalling first
- Editing package.json manually without reinstalling
- Using non-existent --save-prod flag
5. You want to install
webpack and babel packages. webpack is needed to run your app, but babel is only needed during development. Which commands correctly install them with proper dependency types?hard
Solution
Step 1: Identify runtime vs development packages
Webpack is needed at runtime, so it should be a regular dependency. Babel is only for development, so it should be a devDependency.Step 2: Use correct npm commands for each
Usenpm install webpackfor runtime dependency andnpm install --save-dev babelfor development dependency.Final Answer:
npm install webpack && npm install --save-dev babel -> Option AQuick Check:
Runtime = npm install, Dev = npm install --save-dev [OK]
Hint: Install runtime normally, dev tools with --save-dev [OK]
Common Mistakes:
- Installing all packages as devDependencies
- Installing dev tools as regular dependencies
- Using --save-dev for runtime packages
