Ever wonder why some projects are bloated with unnecessary tools after deployment?
Installing packages (dependencies vs devDependencies) in Node.js - Why You Should Know This
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you build a Node.js project and manually download every library you need, then copy them into your project folder. You also mix tools needed only during development with those needed in production.
This manual way is slow, confusing, and risky. You might forget to include a library, or accidentally ship development tools to users, making your app bigger and slower.
Using package managers with dependencies and devDependencies lets you clearly separate what your app needs to run from what you need to build it. This keeps your project organized and efficient.
Download lodash and build tools manually and copy to project folder
npm install lodash --save npm install eslint --save-dev
This separation makes your app lighter in production and your development smoother and safer.
When deploying a website, only the runtime libraries are included, while testing and building tools stay behind, speeding up deployment and reducing errors.
Manually managing packages is slow and error-prone.
Dependencies are for runtime; devDependencies are for development only.
Using this separation keeps projects clean, efficient, and easier to maintain.
Practice
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]
- Using --save-dev for packages needed at runtime
- Confusing uninstall with install commands
- Assuming npm update installs packages
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]
- Omitting --save-dev for dev dependencies
- Using --prod which is not a valid flag for devDependencies
- Installing globally instead of locally
package.json snippet, which package will NOT be installed when running npm install --production?
{
"dependencies": {
"express": "^4.18.2"
},
"devDependencies": {
"mocha": "^10.2.0"
}
}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]
- Assuming all packages install regardless of flags
- Confusing dependencies with devDependencies
- Thinking --production installs devDependencies
npm install --save-dev eslint but later realize you need eslint as a regular dependency. What is the best way to fix this?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]
- Trying to install without uninstalling first
- Editing package.json manually without reinstalling
- Using non-existent --save-prod flag
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?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]
- Installing all packages as devDependencies
- Installing dev tools as regular dependencies
- Using --save-dev for runtime packages
