yarn global

在你的操作系统上全局安装软件包。

yarn global <add/bin/list/remove/upgrade> [--prefix]

yarn global 是用于许多命令的前缀,例如 addbinlistremove。它们的行为与其正常版本相同,只是它们使用全局目录来存储包。global 命令使可执行文件可在你的操作系统上使用。

¥yarn global is a prefix used for a number of commands like add, bin, list and remove. They behave identically to their normal versions except that they use a global directory to store packages. The global command makes executables available to use on your operating system.

注意:与 npm 中的 --global 标志不同,global 是必须紧跟在 yarn 之后的命令。输入 yarn add global package-name 将在本地添加名为 globalpackage-name 的包,而不是全局添加 package-name

¥Note: Unlike the --global flag in npm, global is a command which must immediately follow yarn. Entering yarn add global package-name will add the packages named global and package-name locally instead of adding package-name globally.

这对于不属于任何单个项目而是用于本地命令的开发者工具非常有用。nodemon 就是一个这样的例子,它可以像这样全局安装:

¥This is useful for developer tooling that is not part of any individual project but instead is used for local commands. One such example is nodemon which can be installed globally like this:

$ yarn global add nodemon --prefix /usr/local
# the `nodemon` command is now available globally:
$ which nodemon
$ /usr/local/bin/nodemon
$ nodemon

定义安装位置

¥Defining install location

yarn global bin 将输出 Yarn 将符号链接安装到已安装的可执行文件的位置。你可以使用 yarn config set prefix <filepath> 配置基本位置。例如,yarn config set prefix ~/.yarn 将确保所有全局包的可执行文件都安装到 ~/.yarn/bin

¥yarn global bin will output the location where Yarn will install symlinks to your installed executables. You can configure the base location with yarn config set prefix <filepath>. For example, yarn config set prefix ~/.yarn will ensure all global packages will have their executables installed to ~/.yarn/bin.

yarn global dir 将打印包含全局 node_modules 的全局安装文件夹的输出。默认情况下将是:~/.config/yarn/global

¥yarn global dir will print the output of the global installation folder that houses the global node_modules. By default that will be: ~/.config/yarn/global.

将安装位置添加到你的 PATH

¥Adding the install location to your PATH

要使用已安装的软件包,必须将安装位置添加到 shell 的 PATH 环境变量中。例如,对于 bash,你可以在 .bashrc 末尾添加此行:

¥To use the installed packages, the install location has to be added to the PATH environment variable of your shell. For bash for example, you can add this line at the end of your .bashrc:

export PATH="$(yarn global bin):$PATH"

了解有关可与 yarn global 一起使用的命令的更多信息:

¥Read more about the commands that can be used together with yarn global:

  • yarn add:添加要在当前包中使用的包。

    ¥yarn add: add a package to use in your current package.

  • yarn bin:显示 Yarn 箱文件夹的位置。

    ¥yarn bin: displays the location of the yarn bin folder.

  • yarn list:列出已安装的软件包。

    ¥yarn list: list installed packages.

  • yarn remove:删除当前包中不再使用的包。

    ¥yarn remove: remove a package that will no longer be used in your current package.

  • yarn upgrade:根据指定范围将软件包升级到最新版本。

    ¥yarn upgrade: upgrade packages to their latest version based on the specified range.

  • yarn upgrade-interactive:与 upgrade 命令类似,但在执行任何升级之前显示过时的软件包,允许用户选择要升级的软件包。

    ¥yarn upgrade-interactive: similar to upgrade command, but display the outdated packages before performing any upgrade, allowing the user to select which packages to upgrade.