【问题标题】:npm git protocol dependenciesnpm git 协议依赖
【发布时间】:2013-02-02 22:46:13
【问题描述】:

在工作中,我们使用 HTTP 代理,并且 git 协议(端口 9418)被拒绝。 我的项目具有 NPM 依赖项,其中一些依赖项具有使用 git 协议的依赖项,例如:

在我的package.json

"dependencies": {
    "jsdoc3" : "git+https://github.com/jsdoc3/jsdoc.git"
}

还有jsdoc3的package.json

"dependencies": {
    "crypto-browserify": "git://github.com/dominictarr/crypto-browserify.git#95c5d505",
    "github-flavored-markdown": "git://github.com/hegemonic/github-flavored-markdown.git"
}

如何获取这些依赖关系,如何告诉 NPM 使用 git+https:// 协议而不是 git:// 协议或能够使用 git 协议?

为了简化我在 Windows 上的操作(在 Linux 上创建 SSH 隧道会更容易),我使用 GIT-Bash。

谢谢

【问题讨论】:

    标签: git npm


    【解决方案1】:

    npm ci 一直在尝试使用ssh://,所以我不得不执行以下操作:

    git config --global url."https://github.com/".insteadOf git@github.com:
    git config --global url."https://".insteadOf ssh://
    

    【讨论】:

      【解决方案2】:

      除了@Nowres 的建议之外,我还必须执行以下操作才能使其正常工作

      git config --global url."https://github.com/".insteadOf git@github.com:
      git config --global url."https://".insteadOf git://
      

      【讨论】:

        【解决方案3】:

        npm wiki 中推荐了两个 git 命令(参考:npm uses git:// and ssh+git:// only by default)。

        git config --global url."https://github.com/".insteadOf git@github.com:
        git config --global url."https://".insteadOf git://
        

        【讨论】:

          【解决方案4】:

          您可以通过以下命令告诉 git 使用 https 而不是 git://:

          git config --global url."https://".insteadOf git://
          

          【讨论】:

            【解决方案5】:

            最后我找到了一个肮脏的解决方案,但效果很好。我修改了 NPM 的代码,用http 协议替换了git 协议(感谢开源)

            npm v1.1.69 中,在文件 npm/lib/cache.js 中,我在函数 addRemoteGit 中添加了以下几行

             // ssh paths that are scp-style urls don't need the ssh://
             if (parsed.pathname.match(/^\/?:/)) {
               u = u.replace(/^ssh:\/\//, "")
             }
            
             //begin trick
             if(/^git:/.test(u)){
                 u = u.replace(/^git/, 'https');
             }
             //end trick
            
             log.verbose("addRemoteGit", [u, co])
            

            【讨论】:

            • 谢谢,url 重写在我的工作代理后面不起作用,但确实如此。
            • 我还必须将此文件中的 git: 更改为 https:npm/node_modules/github-url-from-username-repo/index.js,但您让我走上了解决同一问题的正确轨道。谢谢!
            【解决方案6】:

            可以在依赖 URL 中指定 git+https://git+http://

            我从

            中获取了以下 package.json
            {
              "name": "Sample package",
              "description": "Pacake for a Stackoverflow question",
              "author": "rk <rk@example.sampletld>",
              "dependencies": {
                "crypto-browserify": "git+https://github.com/dominictarr/crypto-browserify.git#95c5d505",
                "github-flavored-markdown": "git+https://github.com/hegemonic/github-flavored-markdown.git"
              },
              "engine": "node 0.4.1"
            }
            

            然后我运行npm installnode_modules 包含以下内容

            C:\Users\myself\node\node_modules>dir
             Volume in drive C is WINDOWS
             Volume Serial Number is 6E7A-96BE
            
             Directory of C:\Users\myself\node\node_modules
            
            18/02/2013  13:57    <DIR>          .
            18/02/2013  13:57    <DIR>          ..
            18/02/2013  13:58    <DIR>          .bin
            18/02/2013  13:57    <DIR>          crypto-browserify
            18/02/2013  13:56    <DIR>          express
            18/02/2013  13:57    <DIR>          github-flavored-markdown
            18/02/2013  13:56    <DIR>          optimist
                           0 File(s)              0 bytes
                           7 Dir(s)  31,641,919,488 bytes free
            
            C:\Users\myself\node\node_modules>
            

            我用 git+http 和 git+https 两种协议都试过了,两者都有效,但裸 http 无法工作,产生错误。

            【讨论】:

            • 我无法控制依赖项的 package.json 文件。在我的 package.json 中,我所有的依赖项都使用 git+https 方案,但问题在于传递依赖项(我的依赖项的依赖项)。
            • 啊,我明白了;在 Windows 环境中设置 SSH 隧道应该不是不可能的,有工具可以做到这一点。如果您无法破解您的直接依赖项以使用 +http 变体。一个可怕的黑客虽然肯定。最后,可以选择为项目制作商业案例并将其提交给您的网络经理以允许 git 流量。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-02-24
            • 2017-10-01
            • 2018-01-14
            • 1970-01-01
            • 2019-04-18
            • 2017-11-24
            相关资源
            最近更新 更多