# dependencies vs devDependencies

> A package.json categorizes dependencies into four buckets. Understanding the exact difference between them is critical for bundle size optimization.

Subject: Package Managers | Lesson 7 of 24 | 4 min read
Interactive lesson: https://codewithanimation.in/package-managers/dependencies-vs-devdependencies
Previous: [Version Ranges: Caret (^) vs Tilde (~)](https://codewithanimation.in/package-managers/version-ranges-caret-tilde.md) | Next: [node_modules Structure & Hoisting](https://codewithanimation.in/package-managers/node-modules-and-hoisting.md)

## The Four Dependency Buckets

A `package.json` file categorizes dependencies into four distinct buckets. Understanding the exact difference between them is critical for bundle size optimization, CI/CD pipeline speed, and security.

dependencies records packages needed by the application at runtime. devDependencies records development, build and test tools. A production build stage may still need those tools; a prepared runtime stage may be able to omit them.

## Strict Contracts and Optional Enhancements

peerDependencies expresses compatibility with a host-provided ecosystem, such as a React version expected by a library. Placement and conflict handling depend on the resolved graph and manager configuration; a peer declaration is not a universal guarantee of one copy.

Finally, **`optionalDependencies`** are non-critical features (often OS specific native bindings); if they fail to compile, the install continues successfully anyway.

## Production Installs and ERESOLVE

Use --omit=dev for an installation stage that does not need development tools. Build first when the compiler or bundler is a devDependency. Omission affects installed files on disk; those requirements can still be resolved in the lockfile.

npm 7 and later install peer dependencies by default. Some incompatible requirements cause ERESOLVE, while resolution and strictness settings affect handling. --legacy-peer-deps ignores peer contracts; it does not repair incompatible APIs.

## Common mistake: The #1 Mistake: Shipping Dev Dependencies

Classify a compiler or bundler according to when the project needs it. Keeping build tools in devDependencies can reduce a prepared runtime installation, but the build stage must still install the tools it uses.

## Tip: NODE_ENV Context

NODE_ENV=production makes omitting development dependencies npm's default in relevant install commands; explicit include/omit configuration can affect the result. Do not omit required build tools before compilation.

## Try it: Dependency Deployment Simulator

Toggle between development and production install modes. Observe which dependencies actually ship to the production server and how peer and optional dependencies behave.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/package-managers/dependencies-vs-devdependencies

## Check yourself

### Question 1

Your server imports a library while serving requests; TypeScript is only used to compile it. Which classification fits?

- A. Both must always be global installs
- B. Compiler in dependencies; runtime library only in devDependencies
- C. Library in dependencies; compiler in devDependencies
- D. Neither belongs in package.json

Correct answer: C

Classify packages by their role. Build tooling can be required during a production build even though it is not needed by the running output.

- Why not A: Global installs do not declare reproducible project requirements.
- Why not B: This reverses their stated roles.
- Why not D: Both are project requirements and should be declared.

### Question 2

A deployment must run a TypeScript build. When is omitting development dependencies appropriate?

- A. Before compilation, even if the compiler is needed
- B. Only after moving every tool to a global install
- C. It is never possible to omit them
- D. After the build, if the runtime output no longer needs them

Correct answer: D

A build stage needs its build tools. A separate runtime stage may omit development dependencies once the required output is produced.

- Why not A: Omitting a required compiler prevents that build step.
- Why not B: Global tools are not required for a staged build.
- Why not C: A prepared runtime stage may not need build/test tools.

### Question 3

Which statement distinguishes peerDependencies from optionalDependencies?

- A. Peers describe host compatibility; optional install failures can be tolerated
- B. Peer requirements guarantee compatible runtime behavior automatically
- C. An optional dependency must always be available at runtime
- D. legacy-peer-deps fixes incompatible library APIs

Correct answer: A

Peer requirements express compatibility with a consumer-supplied ecosystem. Optional dependencies may fail installation, so callers must handle their absence.

- Why not B: A declared requirement does not prove runtime compatibility.
- Why not C: Optional failures or omissions require absence handling.
- Why not D: Ignoring peer requirements does not repair incompatible code.

## Remember this

- Classify runtime packages and development/build tools by their roles.
- A build stage can need devDependencies even if the final runtime stage uses --omit=dev.
- Peer requirements express compatibility; ignoring them does not make incompatible packages work.
- If an `optionalDependency` fails to install (e.g., native compilation error), npm ignores the failure and succeeds.
