DEV Community

Hemant Govekar
Hemant Govekar

Posted on

Types of dependencies in package.json file

Let’s break down each type of dependency in package.json in detail:

1. Dependencies ("dependencies")

  • These are the essential packages required for your project to run in production.
  • They are installed automatically when you run npm install or yarn install.
  • Example: If you're building a React application, react and react-dom would be dependencies.
  • Defined like this in package.json:
  "dependencies": {
    "react": "^18.0.0",
    "redux": "^4.1.0"
  }
Enter fullscreen mode Exit fullscreen mode
  • When you deploy your application, these packages are included.

2. DevDependencies ("devDependencies")

  • These packages are only needed during development (e.g., testing tools, linters, build tools).
  • They won’t be included in the production build.
  • Example: Testing frameworks like jest or bundlers like webpack.
  • Installed with:
  npm install --save-dev jest
Enter fullscreen mode Exit fullscreen mode
  • Defined in package.json like this:
  "devDependencies": {
    "jest": "^29.0.0",
    "eslint": "^8.0.0"
  }
Enter fullscreen mode Exit fullscreen mode

3. PeerDependencies ("peerDependencies")

  • These are dependencies that the package expects to exist in the user's environment.
  • They don’t get installed automatically but must be provided by the host project.
  • Example: A UI library might list react as a peer dependency so users can use their own version.
  • Defined like this:
  "peerDependencies": {
    "react": "^18.0.0"
  }
Enter fullscreen mode Exit fullscreen mode
  • If a required peer dependency isn’t installed, a warning will be displayed.

4. OptionalDependencies ("optionalDependencies")

  • These dependencies are not mandatory but can enhance functionality.
  • If an optional dependency fails to install, the package manager won’t break the installation process.
  • Example: A package might include a database driver as an optional dependency that users can choose to install.
  • Defined like this:
  "optionalDependencies": {
    "sqlite3": "^5.0.0"
  }
Enter fullscreen mode Exit fullscreen mode

5. BundledDependencies ("bundledDependencies")

  • These dependencies are packaged together when distributing the application.
  • Useful when creating a CLI tool or library that should bundle certain dependencies for consistency.
  • Example:
  "bundledDependencies": ["lodash", "moment"]
Enter fullscreen mode Exit fullscreen mode
  • Ensures that these dependencies are always included.

Each type of dependency serves a different purpose in managing the project’s requirements effectively.

Top comments (0)