如何更改使用的纱线版本?

How do I change the version of yarn used?

我使用 Homebrew 安装 yarn。 运行yarn -v说明我目前使用的是0.23.2。我运行brew upgrade yarn获取最新版本,也就是0.24.6.

Homebrew升级成功后yarn,我又运行yarn -v,但是版本还是0.23.2。我如何更改我正在 运行ning 的 `yarn 版本?

我找到了答案。它不是最漂亮的,但由于 yarn 总是告诉我最新的版本是什么,我可以使用 npm 安装最新版本。

如果最新的是0.24.6

npm install --global yarn@.24.6

编辑:

根据yarn的官方文档,install/upgrade的方法是:

brew install yarn

brew upgrade yarn

https://yarnpkg.com/en/docs/install#mac-stable

2021 年 12 月更新:

遗憾的是,brew 开关已在 Homebrew 2.6.0(2020 年 12 月)

中弃用
$ brew switch
Error: Unknown command: switch

TLDR,要切换版本:

brew unlink yarn
brew link yarn@<new_version>

旧解决方案:

假设你安装了其他版本,你可以运行

brew switch yarn <old_version>

列出您已安装的版本:

brew list --versions yarn

您可以使用自制软件和 yarn 公式 URL 安装旧版本的 yarn,然后 brew switch 根据需要在 yarn 版本之间安装。完美运行!感谢 github 用户 robertmorgan。

  1. First off, if you already have a version installed, unlink it from brew running the brew unlink yarn command in your terminal.

  2. Next, in a web browser, find the Pull Request that has been merged which contained the formula (version) of Yarn that you want to install.

  3. View the files changed in that Pull Request - there should be one for Formula/yarn.rb.

  4. Click the "View" button for the Formula/yarn.rb file to see the whole contents of the file for that commit.

  5. Click the button to view the "Raw" version of that file. This will open a url which should start with https://raw.githubusercontent.com/....

    This is the URL that you will need for the next step - so copy the complete URL to your clipboard.

  6. Back in your terminal window, use the command brew install followed by the URL that you've copied.

    e.g. to install v1.6.0 of yarn it would be:

    brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/fba7635ab69384ac980c635483a7af825bc06088/Formula/yarn.rb
    

You can then verify the versions available to Homebrew by running: brew list --versions yarn, and switch between versions using brew switch yarn VERSION_NUMBER

来源:https://github.com/yarnpkg/yarn/issues/1882#issuecomment-421372892

轻松安装并在任意数量的 yarn 版本之间切换。

https://github.com/tophat/yvm

这是一种仅使用 curlbash 的方法:

curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version 0.24.6  # or another version

无论您是否安装了纱线,这都有效。

您最好的选择是使用 yarn version manager.

安装:

curl -fsSL https://raw.githubusercontent.com/tophat/yvm/master/scripts/install.sh | bash

单次使用:

yvm exec <version> <command>

或者切换您当前的 运行 纱线版本

yvm use <version>
yarn --version
yarn policies set-version <version number>

https://github.com/yarnpkg/yarn/issues/7146#issuecomment-477809216

要在 macOS 上更新版本,请使用以下命令:

$ brew upgrade yarn

您可以使用

yarn set version <version>

比如你想要最新的版本,可以运行

yarn set version latest

您也可以将其设置为特定的数字,例如

yarn set version 1.22.1

您可以在 official website 查看完整文档。


注意 1:有一个错误阻止您从 yarn 2 切换回 yarn 1。这里有解决方案 github issue

注意 2:我没有使用 brew,但这个解决方案应该仍然有效。

上面的一些答案似乎不再有效。以下是我在 2021 年 4 月安装不同版本的方法:

brew unlink yarn@1.6.0(如果您已经安装了一个版本)

brew extract --version 1.22.4 yarn homebrew/cask

brew install yarn@1.22.4

yarn -v