Topic 26 of 55
Production vs DevDependencies
Overview
Dependencies are split into two categories. `dependencies` are packages your app needs to run in production (like Express, React, Mongoose). `devDependencies` are packages only needed during local development (like TypeScript, Jest, ESLint).
Syntax
bash
# Install as a Production Dependency
npm install axios
# Shorthand: npm i axios
# Install as a Development Dependency
npm install --save-dev jest typescript
# Shorthand: npm i -D jest typescript
# In production environments (like AWS or Heroku), you run:
npm install --production
# This skips installing devDependencies, saving time and disk space.Common Pitfalls
- If you accidentally put a production module (like `express`) into `devDependencies`, your app will crash when deployed to a server because the server runs `npm install --production`.
- Don't bloat your production server with gigabytes of testing libraries. Keep them in `devDependencies`.
Real-World Example
A typical split of responsibilities in a Node app:
example
bash
{
"dependencies": {
"bcrypt": "^5.1.0", // Needed to hash passwords on the live server
"express": "^4.18.2", // Needed to handle live HTTP requests
"pg": "^8.11.0" // Needed to connect to the live database
},
"devDependencies": {
"@types/express": "^4.17.17", // Only needed to compile TypeScript
"nodemon": "^2.0.22", // Only needed to auto-restart server while coding
"prettier": "^2.8.8" // Only needed to format code before committing
}
}