Learning navigation

Code with Animation

dependencies vs devDependencies

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

Package Managers4 min readConcept 7 of 24

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.

package.json
{
  "name": "my-app",
  "dependencies": {
    "react": "^18.2.0",
    "lodash": "^4.17.21"
  },
  "devDependencies": {
    "jest": "^29.5.0",
    "typescript": "^5.0.4"
  },
  "peerDependencies": {
    "react-dom": "^18.2.0"
  },
  "optionalDependencies": {
    "fsevents": "^2.3.2"
  }
}
node_modules/
react
dep
lodash
dep
react-dom
peer
jest
dev
typescript
dev
fsevents
optional

ERESOLVE unable to resolve dependency tree

Conflicting peer dependency: react-dom@"^17.0.0" from the root project disagrees with "^18.2.0".

Standard install pulls down everything required for local development and testing.

Check yourself

Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.

  1. 1Your server imports a library while serving requests; TypeScript is only used to compile it. Which classification fits?
  2. 2A deployment must run a TypeScript build. When is omitting development dependencies appropriate?
  3. 3Which statement distinguishes peerDependencies from optionalDependencies?

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.

Done with this concept?

Mark it complete to track your progress. No login needed.