devDependencies
- Node.js course
- Intermediate
- Lesson 72/116
- 5 min
Learn development packages with devDependencies.
devDependencies
devDependencies is for packages needed only during development: test frameworks, linters, build tools. When running in production these packages are not needed.
For example, jest is a test framework; tests are not run in production. So it is added to devDependencies with npm install --save-dev jest. express runs in production, so it goes into dependencies. When you install with npm install --production, devDependencies are skipped, which keeps the production image small. This way packages are separated by purpose and the deployment is optimized.
The --save-dev flag
The way to add a package to devDependencies is the --save-dev flag. Its short form is -D. When you run npm install --save-dev jest, jest is both downloaded and added to the devDependencies section of package.json.
Without a flag, npm install jest adds the package to dependencies, meaning it counts as a package loaded in production. That is the wrong place for a test framework. It is important to think about which section fits: is it needed in production? If the answer is no, use --save-dev. Then npm install --production skips this package.
Practice Print whether jest is in devDependencies
Define a string named content with the value {"dependencies":{"express":"^4.18.0"},"devDependencies":{"jest":"^29.0.0"}}. Parse it into an object with JSON.parse. Print "jest" in pkg.devDependencies with console.log.
Starter code
// Define the package.json content (with dependencies and devDependencies)
// Parse it into an object with JSON.parse
// Print whether jest is in devDependencies
// write hereSample solution
const content = '{"dependencies":{"express":"^4.18.0"},"devDependencies":{"jest":"^29.0.0"}}';
const pkg = JSON.parse(content);
console.log("jest" in pkg.devDependencies);Expected output
true
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Match each package to its section
| Term | Meaning |
|---|---|
jest |
Test framework (dev) |
express |
Web framework (prod) |
eslint |
Code linter (dev) |
lodash |
Utility library (prod) |
2 Which one belongs in devDependencies?
devDependencies- mongoose (database)
- express (web framework)
- lodash (utility)
- jest (test framework) Correct answer
Explanation: Test frameworks are needed only during development.
3 Add to devDependencies
npm install ____ jest
npm install --save-dev jest
Explanation: --save-dev adds jest to devDependencies.