Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: clarify what peerDependenciesMeta does #7433

Merged
merged 3 commits into from
May 1, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions docs/lib/content/configuring-npm/package-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -818,11 +818,12 @@ to express this. If you depend on features introduced in 1.5.2, use

### peerDependenciesMeta

When a user installs your package, npm will emit warnings if packages
specified in `peerDependencies` are not already installed. The
`peerDependenciesMeta` field serves to provide npm more information on how
The `peerDependenciesMeta` field serves to provide npm more information on how
your peer dependencies are to be used. Specifically, it allows peer
dependencies to be marked as optional.
dependencies to be marked as optional. Npm would not automatically install
wraithgar marked this conversation as resolved.
Show resolved Hide resolved
optional peer dependencies. This allows you to
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Npm would not automatically install optional peer dependencies.

This is a very confusing and potentially misleading sentence. At a glance it reads that npm does not install optional peer dependencies. In fact it does, it just does not raise an error if they can not be installed.

Copy link
Contributor Author

@xuhdev xuhdev May 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this myself, npm seems to not install optional peer dependencies by default. I"m on npm version 10.7.0.

  1. mkdir my-package && cd my-package
  2. Save the following file to package.json:
{
  "name": "tea-latte",
  "version": "1.3.5",
  "peerDependencies": {
    "lodash": "4.17.21",
    "core-js": "3.37.0"
  },
  "peerDependenciesMeta": {
    "lodash": {
      "optional": true
    }
  }
}
  1. npm install.

Observe that node_module doesn't contain lodash, but it contains core-js.

Then:

  1. npm pack (which generates tea-latte-1.3.5.tgz)
  2. mkdir ../user && cd ../user
  3. npm install ../my-package/tea-latte-1.3.5.tgz

Same effect, only core-js is installed, not lodash.

Copy link
Member

@wraithgar wraithgar May 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well then my memory of the situation was wrong. I must have gotten optional and optional peer dependencies confused. Thanks.

integrate and interact with a variety of host packages without requiring
all of them to be installed.

For example:

Expand All @@ -842,11 +843,6 @@ For example:
}
```

Marking a peer dependency as optional ensures npm will not emit a warning
if the `soy-milk` package is not installed on the host. This allows you to
integrate and interact with a variety of host packages without requiring
all of them to be installed.

### bundleDependencies

This defines an array of package names that will be bundled when publishing
Expand Down
Loading