node.js 如何使用 Yarn 安装多个全局包的列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43895201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to install a list of many global packages with Yarn
提问by jleeothon
yarn install -hsuggests that the -g(global) option is DEPRECATED. How am I supposed to indicate that I want a bunch of packages (from package.json/ yarn.lockfiles) to be installed globally?
yarn install -h表明-g(全局)选项是DEPRECATED. 我应该如何表明我想要全局安装一堆软件包(来自package.json/yarn.lock文件)?
Options I saw:
我看到的选项:
yarn global [command]has things such aslsandaddbut notinstall.addonly works with particular package names, if I understand correctly. I already have myyarn.lockfile ready, I don't want to repeat myself on the command line.yarn global addeach package one by one. Now my list of packages would be imperative instead of declarative.
yarn global [command]有诸如lsandaddbut not 之类的东西install。add如果我理解正确,则仅适用于特定的包名称。我已经yarn.lock准备好了我的文件,我不想在命令行上重复自己。yarn global add每个包裹一一。现在我的包列表将是命令式的,而不是声明式的。
Specifically, I'd like to use one executable from one of those packages.
具体来说,我想使用其中一个包中的一个可执行文件。
采纳答案by Aurora0001
How am I supposed to indicate that I want a bunch of packages (from package.json / yarn.lock files) to be installed globally?
我应该如何表明我想要全局安装一堆包(来自 package.json / yarn.lock 文件)?
You shouldn't. Installing globally is discouraged by Yarn, and there are very few situations where it's necessary, or even helpful.
你不应该。Yarn 不鼓励全局安装,并且很少有必要甚至有用的情况。
As noted in the documentation:
For the vast majority of packages it is considered a bad practice to have global dependencies because they are implicit. It is much better to add all of your dependencies locally so that they are explicit and anyone else using your project gets the same set of dependencies.
If you are trying to use a CLI tool that has a bin you can access these in your ./node_modules/.bin directory.
对于绝大多数包而言,拥有全局依赖项被认为是一种不好的做法,因为它们是隐式的。最好在本地添加所有依赖项,以便它们是显式的,并且使用您的项目的任何其他人都会获得相同的依赖项集。
如果您尝试使用带有 bin 的 CLI 工具,您可以在 ./node_modules/.bin 目录中访问它们。
But I really, really want to!
但我真的,真的很想!
If you reallydon't want to listen to the advice given, use
如果你真的不想听所给出的建议,请使用
yarn global add <package>
However, don't expect to easily install a huge list of dependencies globally—it's hard to do by design, because it's not a good idea.
但是,不要指望在全球范围内轻松安装大量依赖项——这在设计上很难做到,因为这不是一个好主意。
Instead, the intended flow with Yarn is:
相反,Yarn 的预期流程是:
- install everything locally, so each project is isolated
- call binaries from
./node_modules/.binwhere possible - avoid global installs—they're a convenience, but not one you should rely on.
- 在本地安装所有东西,所以每个项目都是孤立的
./node_modules/.bin尽可能调用二进制文件- 避免全局安装——它们很方便,但不是你应该依赖的。
回答by vijay
Simply type
只需输入
yarn global add nodejs
回答by laurent
For those interested, here's a way to install and manage global applications installed via yarn.
对于那些感兴趣的人,这里有一种安装和管理通过 yarn 安装的全局应用程序的方法。
First create a directory which will contain the applications, for example ~/.yarn-global:
首先创建一个包含应用程序的目录,例如~/.yarn-global:
mkdir ~/.yarn-global
cd ~/.yarn-global
Then install your application from here:
然后从这里安装您的应用程序:
yarn add yourapp
Finally open your profile file, i.e. .bashrcor .bash_profileand add the path to the bin directory:
最后打开您的配置文件,即.bashrcor.bash_profile并将路径添加到 bin 目录:
export PATH="$PATH:$HOME/.yarn-global/node_modules/.bin"
From now on, any application you install in this directory will be available from anywhere in your shell.
从现在开始,您在此目录中安装的任何应用程序都可以在您的 shell 中的任何位置使用。
Once this is done, you can even create a yarn-globalutility script that will only operate in this .yarn-global directory. For example:
完成此操作后,您甚至可以创建一个yarn-global仅在此 .yarn-global 目录中运行的实用程序脚本。例如:
sudo vim /usr/bin/yarn-global
sudo chmod 755 /usr/bin/yarn-global
And the script content would be:
脚本内容将是:
#!/bin/bash
cd "$HOME/.yarn-global"
yarn ""
Now you can do yarn-global add someapp, yarn-global upgrade someapp, etc.
现在你可以做yarn-global add someapp,yarn-global upgrade someapp等等。
回答by Nsikak
Yarn adds all global package to a .yarn folder in your home: ~/.yarn/bin
Yarn 将所有全局包添加到您家中的 .yarn 文件夹中: ~/.yarn/bin
so you have to export it as:
所以你必须将它导出为:
export PATH="$PATH:$HOME/.yarn/bin"
or add it to the .bashrc file in your home folder
或将其添加到您的主文件夹中的 .bashrc 文件中
回答by B T
npm install -g markdown-toc
npm install -g markdown-toc
Yarn decided not to support this functionality.
Yarn 决定不支持此功能。

