【问题标题】:How to install package from github repo in Yarn如何在 Yarn 中从 github repo 安装包
【发布时间】:2017-09-10 17:35:14
【问题描述】:

当我使用npm install fancyapps/fancybox#v2.6.1 --save 时,将安装 v2.6.1 标签的 fancybox 包。 docs

中描述了这种行为

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

这个命令是正确的选择吗?在yarn docs 中与这种格式无关。

yarn add fancyapps/fancybox#v2.6.1

【问题讨论】:

    标签: node.js yarnpkg


    【解决方案1】:

    这里有描述:https://yarnpkg.com/en/docs/cli/add#toc-adding-dependencies

    例如:

    yarn add https://github.com/novnc/noVNC.git#0613d18
    

    【讨论】:

      【解决方案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
      

      【讨论】:

      • 但不能使用 git@xxx 样式
      • 如果将 SSH 选项添加到此答案中,我们可能会丢失所有其他选项。
      • 可能还需要添加 dist 文件夹来解析模块。
      • 对于带有私有存储库的 SSH,我发现我们需要添加用户 git。例如:yarn add ssh://git@github.com/fancyapps/fancybox#3.0
      • 这在 Yarn 2 中不起作用;您需要在网址前添加my-package-name@。见这里:github.com/yarnpkg/berry/issues/994#issuecomment-591853492
      【解决方案3】:

      对于 ssh 样式的 url,只需在 url 前添加 ssh:

      yarn add ssh://<whatever>@<xxx>#<branch,tag,commit>
      

      【讨论】:

      • yarn 拒绝遵守常见的git ssh url 语法时,这对我有用:有效:yarn add ssh://git@github.com:my-org/my-repo#commit_hash 无效:yarn add git@github.com:my-org/my-repo#commit_hash
      • 有没有办法做到这一点,但从 package.json 和 yarn install 开始?
      【解决方案4】:

      对于GitHub(或类似的)私有仓库:

      yarn add 'ssh://git@github.com:myproject.git#<branch,tag,commit>'
      npm install 'ssh://git@github.com:myproject.git#<branch,tag,commit>'
      

      【讨论】:

        【解决方案5】:

        我在 github 存储库中使用这种短格式:

        yarn add github_user/repository_name#commit_hash

        【讨论】:

          【解决方案6】:

          纱线 2

          Yarn 2 从远程 URL 安装略有改变。具体来说,remote URLs must be prefixed with the package name。所以对于 github 这意味着:

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

          确保 &lt;package name&gt; 与 repo 的 package.json 文件的 "name" 字段中的值匹配。

          要定位特定分支,请通过 URL 片段添加 head=&lt;branch&gt;commit=&lt;full commit hash&gt;

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

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

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

          【讨论】:

          • 如何从 PR 安装?
          • @CodeBy 将 /#head= 替换为 PR 的源用户/repo/branch
          猜你喜欢
          • 2020-10-17
          • 2019-07-31
          • 1970-01-01
          • 2020-02-14
          • 1970-01-01
          • 2013-11-13
          • 2014-12-29
          • 2019-08-31
          • 2016-09-02
          相关资源
          最近更新 更多