npm在package.json中依赖安装私有github存储库

时间:2014-04-22 04:34:21

标签: node.js github dependencies npm package.json

我试图通过npm安装github私有存储库,其中包含其他私有github存储库作为依赖。

已经尝试了很多方法和帖子,但没有一个工作。这就是我正在做的事情:

npm install git+https://github.com/myusername/mygitrepository.git
package.json中的

就像:

"dependencies": {
    "repository1name": "git+https://github.com/myusername/repository1.git",
    "repository2name": "git+https://github.com/myusername/repository2.git"
}

这样做的正确方法是什么?

11 个答案:

答案 0 :(得分:129)

试试这个:

"dependencies" : {
  "name1" : "git://github.com/user/project.git#commit-ish",
  "name2" : "git://github.com/user/project.git#commit-ish"
}

你也可以试试这个,其中visionmedia / express是name / repo:

"dependencies" : {
   "express" : "visionmedia/express"
}

或(如果存在npm包模块):

"dependencies" : {
  "name": "*"
}

取自NPM docs

答案 1 :(得分:76)

以下在我需要的所有场景中都运行良好:

"dependencies": {
"GitRepo": "git+https://<token-from-github>:x-oauth-basic@github.com/<user>/<GitRepo>.git"
}

答案 2 :(得分:58)

对于那些来到公共目录的人,来自npm docs:https://docs.npmjs.com/files/package.json#git-urls-as-dependencies

Git URL as Dependencies

Git网址可以是以下形式:

git://github.com/user/project.git#commit-ish
git+ssh://user@hostname:project.git#commit-ish
git+ssh://user@hostname/project.git#commit-ish
git+http://user@hostname/project/blah.git#commit-ish
git+https://user@hostname/project/blah.git#commit-ish

commit-ish可以是任何标记,sha或分支,可以作为git checkout的参数提供。默认值为master。

答案 3 :(得分:47)

接受的答案有效,但我不想将安全令牌粘贴到package.json

我在其他地方找到了它,只需运行这个一次性命令as documented in the git-config manpage

git config --global url."https://${GITHUB_TOKEN}@github.com/".insteadOf git@github.com:

GITHUB_TOKEN可以设置为environmnet变量或直接粘贴

然后我安装私有github repos,如:npm install user/repo --save

也适用于Heroku,只需在git config ...中将上述heroku-prebuild命令设置为package.json脚本,并将GITHUB_TOKEN设置为Heroku配置变量。

答案 4 :(得分:33)

人们指出,有多种方法可以做到,但最短的版本是:

// from master
"depName": "user/repo",

// specific branch
"depName": "user/repo#branch",

// specific commit
"depName": "user/repo#commit",

e.g。

"dependencies" : {
  "hexo-renderer-marked": "amejiarosario/hexo-renderer-marked#patch-1",
  "hexo-renderer-marked": "amejiarosario/hexo-renderer-marked#2249507",
  "hexo-renderer-marked": "amejiarosario/hexo-renderer-marked",
}

答案 5 :(得分:23)

"dependencies": {
  "some-package": "github:github_username/some-package"
}

或只是

"dependencies": {
  "some-package": "github_username/some-package"
}

https://docs.npmjs.com/files/package.json#github-urls

答案 6 :(得分:5)

由于Git使用了curl,您可以将 ~/.netrc 文件与凭据一起使用。对于GitHub,它看起来像这样:

machine github.com
  login <github username>
  password <password OR github access token>

如果您选择使用access tokens,则可以从以下位置生成:

  

设置 - &gt;开发者设置 - &gt;个人访问令牌

如果您在自己的公司中使用Github Enterprise,这也应该有效。只需将您的企业github网址放在machine字段中即可。

答案 7 :(得分:1)

对于我的私有存储库引用,我不想包含安全令牌,并且没有其他简单(即仅在package.json中指定)工作。这是做了什么工作:

  1. 去GitHub.com
  2. 导航到私人存储库
  3. 点击“克隆或下载”和复制的网址(与上述示例不符)
  4. 添加了#commit-sha
  5. Ran npm install

答案 8 :(得分:1)

此外,为了使密钥的访问安全

  1. 在 package.json 所在的同一目录级别创建 .env 文件。
  2. 在 .env 文件中提及 PERSONAL_ACCESS_TOKEN=*******************************
  3. 不要忘记将“.env”添加到 .gitingore 列表中,这将防止在您将 git 提交到您的存储库时将密钥暴露给外界。
  4. 现在您可以在 package.json 中添加您的依赖项,如下所示,
<块引用>

Package.json

<块引用>

“依赖项”:{ ... "my-private-github-repo": "git+https://${ENV.PERSONAL_ACCESS_TOKEN}@github.com/USER/abcd-repo-3.4.0.git", ... }

还有其他使用“DOTENV”npm 包的方法,但是当我们尝试解决“Github”包依赖性时,它无能为力。以上似乎是直接的解决方案。

答案 9 :(得分:0)

这是有关如何使用Github令牌而不在package.json文件中发布的更详细的版本。

  1. 创建个人github访问令牌
  2. 在〜/ .gitconfig中重写设置网址
git config --global url."https://<TOKEN HERE>:x-oauth-basic@github.com/".insteadOf https://x-oauth-basic@github.com/
  1. 安装专用存储库。详细日志级别,用于调试访问错误。
npm install --loglevel verbose --save git+https://x-oauth-basic@github.com/<USERNAME HERE>/<REPOSITORY HERE>.git#v0.1.27

如果无法访问Github,请尝试运行git ls-remote ...

的命令npm install will print

答案 10 :(得分:0)

还有SSH Key - Still asking for password and passphrase

在没有本地钥匙串的情况下使用ssh-add ~/.ssh/id_rsa

这避免了麻烦令牌。

相关问题