The difference between dependencies, devDependencies and peerDependencies

In Node.js projects, we usually specify the required dependency packages in the dependencies, devDependencies and peerDependencies fields of the package.json file, but what is the difference between these three fields?

npm & package.json

1. dependencies

Specified in the dependencies field are the dependency packages required when the project is running, that is, the dependencies required in the production environment. These dependencies will be installed in the production environment and packaged into the final release version. Generally speaking, these dependencies refer to libraries and frameworks closely related to the project, such as express, lodash, axios, etc. When we use npm install to install the project, the dependency packages specified in dependencies will also be automatically installed.

Usage scenarios: Applicable to the main functional modules, frameworks and necessary third-party libraries of the project. These dependencies are deployed with the application and run in the production environment.

2. devDependencies

The devDependencies field specifies the dependency packages required in the development environment. These dependencies are typically used by developers when writing and testing code and are not packaged into the final release version. Generally speaking, these dependency packages are used for building, testing, debugging, etc., such as Babel, Webpack, Mocha, etc. When we use npm install to install the project, the dependency packages specified in devDependencies will not be installed. If you need to install dependencies in devDependencies, you need to use npm install --dev.

Usage scenarios: Suitable for auxiliary tools, testing frameworks, build tools, code quality inspection tools, etc. in the development process. These dependencies do not affect the actual running of the application and are only used in the development environment.

3. peerDependencies

Specified in the peerDependencies field is the version number range of other packages that the project depends on. These dependencies will require the installer to manually install the required versions when installing the project. peerDependencies are usually used to inform users about the versions of certain libraries or frameworks that the project depends on when running, and these libraries or frameworks have been installed globally or installed outside the project. peerDependencies can ensure that the installed library version is consistent with the version that the project depends on, thereby reducing version compatibility issues.

scenes to be used:

  1. Developing a library or module that depends on a specific version of an external library: If you are developing a library or module that depends on a specific version of an external library for certain functionality, but you do not want these external libraries to be included in your library, then You can specify these external libraries as peerDependencies in your package.json.
  2. Version compatibility: Sometimes, different libraries may depend on different versions of the same external library, which may lead to version conflicts. By using peerDependencies in your library, you can ensure that users will meet the version requirements of these dependencies when installing your library, thereby avoiding version conflicts.
  3. Library pluggability: If your library needs to work with other libraries or modules, and these libraries or modules may exist in the form of plug-ins or extensions in the project, you can use peerDependencies to ensure that the plug-in is compatible with your library.
  4. Provide recommended dependencies: Sometimes, you may want to provide some recommended dependencies for users, although these dependencies are not mandatory. In this case, you can list these dependencies as peerDependencies, and users can decide whether to install them according to their own needs.

You need to pay attention to the following points when using peerDependencies :

  • Using peerDependencies does not automatically install dependencies, it just tells the user that these external dependencies need to be installed and ensures version compatibility.
  • When using peerDependencies , users need to manually install the required external dependencies in order to work properly with your library or module.
  • To avoid conflicts and confusion, it is recommended that the external dependencies that consumers need to install and the version requirements be clearly stated in the documentation.

4. Summary

{
    
    
  "dependencies": {
    
    
    "@ant-design/icons": "^5.0.1",
    "ahooks": "^3.7.5",
    "antd": "^5.2.3",
    "axios": "^1.3.4",
    "dayjs": "^1.11.7",
    "mobx": "^6.8.0",
    "mobx-react": "^7.6.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-router-dom": "^6.8.2"
  },
  "devDependencies": {
    
    
    "@vitejs/plugin-react": "^3.1.0",
    "eslint": "^8.35.0",
    "msw-tools": "^1.0.52",
    "prettier": "^2.8.4",
    "sass": "^1.58.3",
    "stylelint": "^15.2.0"
  },
  "peerDependencies": {
    
    
    "react": "^18.2.0",
    "react-dom": "^18.2.0"
  }
}

The difference between the above three fields is:

  • Dependencies represent the dependencies necessary for the project to run and will be packaged into the final release version.

  • devDependencies represent dependencies required in the project's development environment and will not be packaged into the final release version.

  • peerDependencies represents the version number range of other packages that the project depends on, and is used to ensure that the installed libraries are consistent with the versions of the libraries that the project depends on.


Welcome to: Tianwen Blog

Guess you like

Origin blog.csdn.net/tiven_/article/details/132562659