dependencies vs devDependencies
A package.json categorizes dependencies into four buckets. Understanding the exact difference between them is critical for bundle size optimization.
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.
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.
ERESOLVE unable to resolve dependency tree
Conflicting peer dependency: react-dom@"^17.0.0" from the root project disagrees with "^18.2.0".
Check yourself
Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.
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
optionalDependencyfails to install (e.g., native compilation error), npm ignores the failure and succeeds.
Done with this concept?
Mark it complete to track your progress. No login needed.