【问题标题】:npm install package from github repo subfolder来自 github repo 子文件夹的 npm 安装包
【发布时间】:2017-01-04 19:11:53
【问题描述】:

当包位于子文件夹中时,是否可以从 github 安装 npm 包?

例如,我们有 Microsoft BotBuilder 存储库: https://github.com/Microsoft/BotBuilder

但我需要在子文件夹“Node/core/”中安装包: https://github.com/Microsoft/BotBuilder/tree/master/Node/core/

那么我该如何用 npm 安装呢?

【问题讨论】:

    标签: git github npm


    【解决方案1】:

    添加到package.json:

    ...
    "scripts": {
      "postinstall": "mkdir BotBuilder; cd BotBuilder; git init; git remote add -f origin https://github.com/Microsoft/BotBuilder.git; git config core.sparseCheckout true; echo \"Node/core\" >> .git/info/sparse-checkout; git pull --depth=1 origin master; cd ..; npm i ./BotBuilder/Node/core/"
      ...
    },
    ...
    

    postinstall 脚本在安装包后运行。

    一步一步来:

    1. 创建文件夹以克隆 repo:mkdir BotBuilder
    2. 进入文件夹:cd BotBuilder
    3. init git repo:git init
    4. 将 git 来源设置为 Microsoft/BotBuilder 存储库:git remote add -f origin https://github.com/Microsoft/BotBuilder.git
    5. 启用sparse checkout:git config core.sparseCheckout true
    6. 添加Node/core到结帐列表:echo "Node/core" >> .git/info/sparse-checkout
    7. 提取部分回购:git pull --depth=1 origin master
    8. 进入您的应用文件夹:cd ..
    9. 安装 BotBuilder:npm i ./BotBuilder/Node/core/

    【讨论】:

    【解决方案2】:

    可能有点偏离主题,但仍然与问题相关

    https://git-scm.com/book/en/v2/Git-Tools-Submodules

    Git 子模块是 git 存储库,您可以在其他存储库中使用(以下称为超级模块)。由于每个子模块都具有通常的各种分支功能和标签,因此每个超级模块都是版本控制的、可插拔的组件,可以单独工作或与超级模块一起开发。

    几个有用的命令

    要添加子模块,请在超级模块中运行以下命令:

    git submodule add <url-to-submodule-repo>
    

    子模块仍然需要初始化并从 repo 中获取:

    git submodule init git submodule update

    可以克隆带有子模块的超级模块,并通过运行获取所有子模块:

    git clone --recursive &lt;url-to-supermodule&gt;

    您可以通过在子模块目录中运行以下命令将上游更改拉到子模块的分支:

    git fetch

    然后运行以下命令更新本地代码:

    git merge

    以下内容将为您的超级模块中的所有子模块获取并合并:

    git submodule update --remote

    如果要跟踪子模块的特定分支,可以使用以下命令:

    git config -f .gitmodules submodule.&lt;my-submodule&gt;.branch fantastic_new_implementation

    如果您已经处理过您的超级模块和子模块并且您推送了您的超级模块,那么对子模块所做的更改将仅存在于本地,而您与之协作的人将不会知道这些更改。 以下命令将在尝试推送您的超级模块之前检查您的子模块是否已被推送

    git push --recurse-submodules=check

    最后,这是一个有用的 ForEach 命令,它允许我们为每个子模块运行一个命令

    git submodule foreach 'git checkout -b featureA

    【讨论】:

      【解决方案3】:

      将指向子文件夹的 github 链接粘贴到 gitpkg 中。然后,您可以使用它与 yarn 或 npm 一起从 github 子文件夹安装包。

      https://gitpkg.now.sh/

      【讨论】:

      • 我感兴趣的回购的恒定 502 错误。例如公共回购:gitpkg.now.sh/vendure-ecommerce/vendure/packages/core?master
      • @s3m3n 我认为您的核心包中的某些安装后脚本或其某些依赖项失败了,我尝试了一点但无法弄清楚:( 我也收到了“500 内部服务器错误” 502
      • 由于目前 GitPkg 对 yarn2 (berry) 没有帮助,临时解决方案可能是:(1) 下载包代码,(2) 构建并使用 npm 打包,(3) 复制我的项目中的结果 tarball,最后 (4) 通过 yarn add &lt;file&gt; 将其添加为依赖项。
      【解决方案4】:

      @Tomasz Jakub Rup 的回答启发。我更新了他的示例并指出了他的示例所基于的 3.0 分支,而是使用了新的 git 功能,称为 sparse-checkout。此功能将节省时间/带宽,因为它不需要提前克隆整个存储库,只会抓取您指定的内容。然而,许多服务器不支持 --filter 选项,该选项可以节省大量空间,但 --depth 1 在许多情况下仍会减少带宽。

      我使用了.tmp_npm 文件夹来最大程度地减少覆盖,并且由于是隐藏文件而可能会被 .gitignored。

      "scripts": {
        "postinstall": "mkdir .tmp_npm; cd .tmp_npm; git init; git clone --filter=blob:none --no-checkout --depth 1 --sparse -b 3.0 https://github.com/Microsoft/BotBuilder.git; cd BotBuilder/; git sparse-checkout init --cone; git sparse-checkout add Node/core; git checkout; cd ../..; npm i .tmp_npm/BotBuilder/Node/core/"
        ...
      },
      

      【讨论】:

        【解决方案5】:

        如果包源托管在 GitHub 上,您可以像这样使用GitPkg

        # using npm:
        npm install https://gitpkg.now.sh/<user>/<project>/<subdir>?<commit-ish>
        # using yarn:
        yarn add https://gitpkg.now.sh/<user>/<project>/<subdir>?<commit-ish>
        

        对于您的特定情况,URL 是这样的:

        https://gitpkg.now.sh/Microsoft/BotBuilder/Node/core?master
        

        在他们的site 上有一个很好的类似向导的表单,可以帮助构建 URL,以及安装它的命令。

        【讨论】:

        • 我有 TAR_BAD_ARCHIVE:无法识别的存档格式
        • @Daniel 请提供更多上下文。你做了什么?
        猜你喜欢
        • 2016-07-09
        • 1970-01-01
        • 2018-06-25
        • 1970-01-01
        • 2016-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多