22

I have angular in my dependencies at 1.5.11:

{
    "dependencies": {
        "angular": "1.5.11",
        "angular-foundation": "0.7.0"
    }
}

angular-foundation happens to depend on angular@>=1.3.0.

Why does Yarn install angular@1.6.9 as a nested dependency of angular-foundation instead of using the project's version? This causes angular to exist twice in the app and doesn't work properly:

node_modules angular (1.5.11) angular-foundation (0.7.0) node_modules angular (1.6.9)

This doesn't happen with npm@5.6.0 - npm uses 1.5.11 for both the app and the package.

Stefan R.
  • 112
  • 15
marekpw
  • 662
  • 2
  • 5
  • 19

2 Answers2

25

You need to use Yarn resolutions for this

https://yarnpkg.com/lang/en/docs/selective-version-resolutions/

So your package.json will become like this

{
  "name": "depdencies",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",

    "dependencies": {
        "angular": "1.5.11",
        "angular-foundation": "0.7.0"
    },
    "resolutions": {
      "**/angular": "1.5.11"
    }
}

Which tells yarn that any child angular dependency will be set to 1.5.11. After updating this run below

$ rm yarn.lock
$ yarn
Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
  • Deleting the yarn.lock is generally not advised, especially on big/"old" projects. The documentation you link advises to only run `yarn install` afterwards – François Jul 04 '23 at 10:16
20

https://classic.yarnpkg.com/en/docs/cli/add/#toc-yarn-add-alias

yarn add <alias-package>@npm:<package>

yarn add react17@npm:react@17

iugo
  • 415
  • 4
  • 8