I'll explain the difference between dependencies and devDependencies in package.json with a diagram and examples.
package.json
+-----------------------------------------+
| |
| "dependencies": { | Production
| "react": "^18.2.0", +-----------------+
| "express": "^4.18.2", | Deployed |
| "axios": "^1.6.0" | Application |
| } | |
| | • react |
| "devDependencies": { | • express |
| "jest": "^29.7.0", | • axios |
| "eslint": "^8.45.0", | |
| "nodemon": "^3.0.1", +-----------------+
| "prettier": "^3.0.0" |
| } |
| | Development
+-----------------------------------------+
| Environment
| +-----------------+
| | Local Dev |
+->| Machine |
| |
| • jest |
| • eslint |
| • nodemon |
| • prettier |
| + dependencies |
+-----------------+
Key Differences
dependencies:
- Required for production runtime
- Included when deploying to production
- Installed with
npm install or npm i package-name
- Example packages:
- Framework libraries (React, Vue)
- Runtime utilities (Lodash)
- Server frameworks (Express)
- Database drivers (MongoDB)
devDependencies:
- Only needed during development
- Not deployed to production
- Installed with
npm install --save-dev or npm i -D package-name
- Example packages:
- Testing frameworks (Jest, Mocha)
- Linters (ESLint)
- Bundlers (Webpack, Vite)
- Formatters (Prettier)
Installation Commands
# Install as dependency
npm install react # Saves to dependencies
# Install as devDependency
npm install --save-dev jest # Saves to devDependencies
When Installing Your Project
# Install all dependencies (both prod and dev)
npm install
# Install only production dependencies
npm install --production
This separation helps keep your production builds lean by excluding development tools from the final deployment.