Nodachisoft Nodachi Sword Icon
  
@Amaji✎ Update January -31-2021

Differences between dependencies, devDependencies, and peerDependenceise in npm package.json

What I write in the page

When you develop a project with npm, you will probably write the dependencies in the package.json file.

When I publish my own npm packages, I write down the dependencies, devDependencies, and peerDependencies. The following is a list of dependencies and their effects that I checked.

Environment and version

  • OS: Windows 10 Home, version 2004
  • node-js: v12.18.2
  • npm: v6.14.5

Example of package configuration

This is what I imagine a package to look like.

package structure

The project created when developing is called the node.js Parent project, and the necessary libraries are called Child projects.

For example, when using the jQuery library, that is a child project.

dependencies

Write the external packages required for the project.

The required libraries listed in the dependencies section of the package.json of the child project (e.g. jQuery) will be installed automatically. They will be automatically installed and placed in the node_modules folder in the parent project.

How to add it to package.json

You can use npm install <package-name> to install the libraries needed to run your project. npm will automatically add them to the package.json file.

devDependencies

child package

You can write a package that is only needed when developing the package itself.

When you create a library that can be used by various projects and release it to the public, you will probably develop the child projects in the figure.

The user (the parent project) may not want to install test libraries or build tools that are only needed when developing the child project.

The libraries that are used only for development should be written in the devDependencies section.

In my case, package.json contains libraries necessary for executing test code such as jest and Jasmine, tools necessary for checking operation, build tools, etc.

How to add it to package.json

You can manually add it to package.json and install it using the npm install command or by specifying the package to be used for development with the option "--save-dev" as in npm install <package-name> --save-dev. The package.json file will be updated and appended.

peerDependenceise

This is useful if you have created your own npm package and want to use it from another project.

child package

Specify the prerequisite packages that are required to run this package (packages that should be installed in the parent project when used). If you are using npm ver.3 or later, when you install the npm package with peerDependencies, it will warn you if any of the libraries listed in this field are not installed. It will not install the parent package by itself.

For example, the npm package "gatsby-remark-text-decoration" assumes that the Gatsby static site generator package is installed, so the package.json is written as shown below.

peerDepenencies
  "peerDependencies": {
    "gatsby": "^2.0.0"
  },

If you try to install this package without having installed Gatsby (npm install), you will get a warning message as shown below.

> npm install gatsby-remark-text-decoration
npm WARN gatsby-remark-text-decoration@1.0.5 requires a peer of gatsby@^2.0.0 but none is installed. You must install peer dependencies yourself.

WARN gatsby-remark-text-decoration@1.0.5 requires a peer of gatsby@^2.0.0 but none is installed. You must install peer dependencies You must install peer dependencies WARN requires a peer of gatsby@^2.0.0 but none is installed. If you want to use it, you'll have to install it manually by yourself . If you want to use it, you have to install it manually yourself.

One last thing

I feel much better about using dependencies, devDependencies, and peerDependencies, which I was unclear about until I created my own npm package!

Websites and references that were very helpful

docs.npmjs.com - Specifying dependencies and devDependencies in a package.json file

docs.npmjs.com - package-json - peerdependencies

Github - gatsby-remark-text-decoration

Rights, etc.

  • npm is a registered trademark of npm, Inc.

Page History

Update Modification
None
 
 
Message sent

Thank you for your message.

Something error has occured!

Sorry. The Error has occurred.We apologize for the inconvenience.Please try again in a few minutes or contact us via DM below.

Twitter:@NodachiSoft_eng
Name:
 
Replay To:
 
Message:
 
Back
Check the content!

Send the following information to us. If you are happy with your submission, please click "Send". If you want to modify it, please click "Back".

Name:
 
Reply To:
 
Message:
 
Enter a confirmation key to make sure that you are not operating from a Robot.
Confirmation Key is 95
Back
 / 
Go to Confirmation
Entry fields
Go to Confirmation

There are 1 articles that may be relevant!

How to make your npm package available to the public for the first time

#Npm✎ 2021-01-31
This is a summary of the steps to release the npm package to the public npm registry for the first time
Table Of Contents
Differences between dependencies, devDependencies, and peerDependenceise in npm package.json
Differences between dependencies, devDependencies, and peerDependenceise in npm package.json
What I write in the page
What I write in the page
Environment and version
Environment and version
Example of package configuration
Example of package configuration
dependencies
dependencies
How to add it to package.json
How to add it to package.json
devDependencies
devDependencies
How to add it to package.json
How to add it to package.json
peerDependenceise
peerDependenceise
One last thing
One last thing
Websites and references that were very helpful
Websites and references that were very helpful
Rights, etc.
Rights, etc.
Page History
Page History
Nodachisoft © 2021