Back to results for “JSON”
Node.js › Git & Project Management

How would you configure a Node.js project's package.json to support both being used as a library (imported by others) and having its own development scripts, distinguishing dependencies from devDependencies correctly?

Advanced

Answer

dependencies lists packages required for the package to actually function at runtime when installed by a consumer (these get installed transitively when someone depends on your package), while devDependencies lists packages needed only for developing, testing, or building the package itself (a test framework, a linter, a bundler) that consumers of the published package never need installed -- correctly categorizing these matters significantly for keeping a published package's actual install footprint minimal for its consumers.
{
  "dependencies": { "express": "^4.18.0" }, // needed at runtime by consumers
  "devDependencies": {
    "jest": "^29.0.0",      // only needed for running this project's own tests
    "eslint": "^8.0.0"      // only needed for linting during development
  }
}
Real-world example A published npm package accidentally listed its testing framework under 'dependencies' instead of 'devDependencies', meaning every single consumer installing the package also unnecessarily downloaded the entire test framework and its own dependency tree; moving it to devDependencies immediately reduced the package's install footprint for everyone depending on it.

Common follow-ups: What's the purpose of a third category, peerDependencies, and when should a package use it instead of a regular dependency?;How does 'npm install --production' or 'npm ci --omit=dev' leverage this distinction in a deployment context?