Node.js
devDependencies
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.
Exercise 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.
Show example solution
const content = '{"dependencies":{"express":"^4.18.0"},"devDependencies":{"jest":"^29.0.0"}}';
const pkg = JSON.parse(content);
console.log("jest" in pkg.devDependencies);
Test yourself
-
Match each package to its section
- jest
- Test framework (dev)
- express
- Web framework (prod)
- eslint
- Code linter (dev)
- lodash
- Utility library (prod)
-
Which one belongs in devDependencies?
devDependencies
- mongoose (database)
- express (web framework)
- lodash (utility)
- jest (test framework)
Show answer
jest (test framework) — Test frameworks are needed only during development.
-
Add to devDependencies
npm install ____ jest
Show answer
npm install --save-dev jest
Next lesson: Semver