berry icon indicating copy to clipboard operation
berry copied to clipboard

[Bug?]: Yarn v3 Incompatibility with 'npm:' prefix in Peer Dependencies

Open arhirani opened this issue 1 year ago • 2 comments

Self-service

  • [ ] I'd be willing to implement a fix

Describe the bug

I am experiencing an issue with Yarn v3 when using the 'npm:' prefix to alias a package in peer dependencies.

In my package.json, I have aliased react-intl as react-intl-next:

"react-intl-next": "npm:react-intl@^5.18.1" However, when I run yarn install, I get the following error:

$packageName: Invalid dependency range for 'react-intl-next'

I believe this is because Yarn v3 is not able to parse the 'npm:' prefix in the version range for peer dependencies. Here is the relevant code from the Yarn v3 binary:

if (typeof range !== `string` || !range.startsWith(WorkspaceResolver.protocol) && !validRange(range)) {
  errors.push(new Error(`Invalid dependency range for '${name}'`));
  range = `*`;
}

This issue is preventing me from being able to correctly install my dependencies. I need to keep the 'npm:' prefix because I need to use a different version of react-intl than the one specified in the package's dependencies or peerDependencies.

Is there a way to make Yarn v3 compatible with the 'npm:' prefix in peer dependencies?

To reproduce

  1. Create a package.json file with the following content:
{
  "name": "my-project",
  "version": "1.0.0",
  "peerDependencies": {
    "react-intl-next": "npm:react-intl@^5.18.1"
  }
}
  1. Run yarn install
  2. Notice the error message: Invalid dependency range for 'react-intl-next'

Environment

- macOS Sonoma 14.3
- Node.js version: 18.17.0
- Yarn version: 3.6.1

Additional context

No response

arhirani avatar Feb 01 '24 11:02 arhirani

This isn't a bug. Peer dependencies only check the version of the package that users provide, not their provenance (ie it doesn't matter whether they come from npm, git, or anything else). As a result, putting anything else than a semver range here will error.

This issue is preventing me from being able to correctly install my dependencies. I need to keep the 'npm:' prefix because I need to use a different version of react-intl than the one specified in the package's dependencies or peerDependencies.

You'll have to rename your own copy of react-intl in your own package. Peer dependencies don't support renaming the packages they lift up.

arcanis avatar Feb 14 '24 08:02 arcanis

I see. Thanks for the the suggestion @arcanis.

arhirani avatar Feb 14 '24 13:02 arhirani

Hi! 👋

It seems like this issue as been marked as probably resolved, or missing important information blocking its progression. As a result, it'll be closed in a few days unless a maintainer explicitly vouches for it.

yarnbot avatar Mar 15 '24 14:03 yarnbot