Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yarn installing multiple versions of the same package

Tags:

npm

yarnpkg

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 [email protected] 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 [email protected] - npm uses 1.5.11 for both the app and the package.

like image 993
marekpw Avatar asked Mar 02 '18 16:03

marekpw


People also ask

Does yarn install multiple versions of the same package?

With npm or yarn, you can install a package under a custom alias. This enables you to install multiple versions of a package in the same project.

How do I install two versions of the same package?

When we install a package using the npm install package-name command, it will download the current stable version of the package inside node_modules folder and add it to package. json file. To install multiple versions of the same package, we need to use the package alias syntax which is supported from the npm v6. 9.0.

Can I install multiple versions of npm package?

The good news is that you can have it done directly via NPM! What can be done in this case is an idea called "package alias". So you can have multiple versions of the same package running in your app and you can work on the upgrades mitigating possible issues.

How do I install multiple dependencies in yarn?

Using npm install npm install all the packages in the package. json . But it looks like you need to specify each package when using yarn add according to https://yarnpkg.com/en/docs/cli/add. yarn add package-name installs the “latest” version of the package.


1 Answers

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
like image 182
Tarun Lalwani Avatar answered Sep 28 '22 06:09

Tarun Lalwani