如何在 Yarn 中从 github repo 安装包

新手上路,请多包涵

当我使用 npm install fancyapps/fancybox#v2.6.1 --save 时,将安装 v2.6.1 标签的 fancybox 包。 文档 中描述了此行为

我想问,如何用 yarn 做到这一点?

这个命令是正确的选择吗?在 yarn docs 中没有关于这种格式的任何内容。

 yarn add fancyapps/fancybox#v2.6.1

原文由 Silver Zachara 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 2.2k
2 个回答

您可以通过指定远程 URL(HTTPS 或 SSH)将任何 Git 存储库(或 tarball)添加为 yarn 的依赖项:

 yarn add <git remote url> installs a package from a remote git repository.
yarn add <git remote url>#<branch/commit/tag> installs a package from a remote git repository at specific git branch, git commit or git tag.
yarn add https://my-project.org/package.tgz installs a package from a remote gzipped tarball.

这里有些例子:

 yarn add https://github.com/fancyapps/fancybox [remote url]
yarn add ssh://github.com/fancyapps/fancybox#3.0  [branch]
yarn add https://github.com/fancyapps/fancybox#5cda5b529ce3fb6c167a55d42ee5a316e921d95f [commit]

(注意:Fancybox v2.6.1 在 Git 版本中不可用。)

要同时支持 npm 和 yarn,可以使用 git+url 语法:

 git+https://github.com/owner/package.git#commithashortagorbranch
git+ssh://github.com/owner/package.git#commithashortagorbranch

原文由 Kasiriveni 发布,翻译遵循 CC BY-SA 4.0 许可协议

纱线 2+

从远程 URL 安装在 Yarn 2 中略有变化。具体来说, 远程 URL 必须以包名作为前缀。所以对于 github 这意味着:

 yarn add '<package name>@https://github.com/<github user>/<github repo>'

确保 <package name> 与 repo 的 package.json 文件的 "name" 字段中的值匹配。

要针对特定分支添加 head=<branch>commit=<full commit hash> 通过 URL 片段:

 yarn add '<package name>@https://github.com/<github user>/<github repo>#head=<branch name>'

如果您尝试从 github 上的 Yarn monorepo 安装单个包,您可以将 workspace=<package name> 添加到 URL 片段:

 yarn add '<package name>@https://github.com/<github user>/<github repo>#head=<branch name>&workspace=<package name>'

原文由 Elliot 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题