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

我尝试了很多方法和帖子,但没有一个是有效的。这是我正在做的:

npm install git+https://github.com/myusername/mygitrepository.git

在包中。Json是这样的:

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

正确的做法是什么?


当前回答

接受的答案是可行的,但我不太喜欢将安全令牌粘贴到package.json中的想法

我在其他地方找到了它,只需运行git-config manpage中记录的这个一次性命令。

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

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

然后我安装私人github回购像:npm install user/repo—save


也适用于Heroku,只需设置上面的git配置…命令作为heroku预构建脚本包。将GITHUB_TOKEN设置为Heroku配置变量。

其他回答

请注意,你试图作为依赖项添加到你的包的github回购。Json文件需要有自己的包。Json文件定义。

接受的答案是可行的,但我不太喜欢将安全令牌粘贴到package.json中的想法

我在其他地方找到了它,只需运行git-config manpage中记录的这个一次性命令。

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

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

然后我安装私人github回购像:npm install user/repo—save


也适用于Heroku,只需设置上面的git配置…命令作为heroku预构建脚本包。将GITHUB_TOKEN设置为Heroku配置变量。

试试这个:

"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文档

以下在我需要的所有场景下工作得很好:

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

对于那些来这里获取公共目录的人,请访问npm文档:https://docs.npmjs.com/files/package.json#git-urls-as-dependencies

Git url作为依赖项

Git url可以是这样的:

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签出的参数提供。默认为master。