我正在尝试通过 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"
}
正确的做法是什么?
试试这个:
"dependencies" : {
"name1" : "git://github.com/user/project.git#commit-ish",
"name2" : "git://github.com/user/project.git#commit-ish"
}
在
commit-ish
的位置,您应该放置提交 sha、标签或分支名称,或者将其完全删除以默认为主分支中的最新提交。
你也可以尝试这个,其中 Visionmedia/express 是名称/仓库:
"dependencies" : {
"express" : "visionmedia/express"
}
或者(如果 npm 包模块存在):
"dependencies" : {
"name": "*"
}
取自 NPM 文档
以下内容在我需要的所有场景中都运行良好:
"dependencies": {
"GitRepo": "git+https://<token-from-github>:[email protected]/<user>/<GitRepo>.git"
}
对于那些来这里获取公共目录的人,请参阅 npm 文档:https://docs.npmjs.com/files/package.json#git-urls-as-dependency
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 可以是任何可以作为参数提供给 git checkout 的标签、sha 或分支。默认为master。
接受的答案有效,但我不太喜欢将安全令牌粘贴到
package.json
中的想法
我在其他地方找到了它,只需运行这个一次性命令如 git-config 手册页中所述。
git config --global url."https://${GITHUB_TOKEN}@github.com/".insteadOf [email protected]:
GITHUB_TOKEN
可以设置为环境变量或直接粘贴
然后我安装私人 github 存储库,例如:
npm install user/repo --save
也适用于 Heroku,只需将上面的
git config ...
命令设置为 heroku-prebuild
中的 package.json
脚本,并将 GITHUB_TOKEN
设置为 Heroku 配置变量。
正如人们指出的那样,有多种方法可以做到这一点,但最短的版本是:
// from master
"depName": "user/repo",
// specific branch
"depName": "user/repo#branch",
// specific commit
"depName": "user/repo#commit",
// private repo
"depName": "git+https://[TOKEN]:[email protected]/user/repo.git"
例如
"dependencies" : {
"hexo-renderer-marked": "amejiarosario/dsa.jsd#book",
"hexo-renderer-marked": "amejiarosario/dsa.js#8ea61ce",
"hexo-renderer-marked": "amejiarosario/dsa.js",
}
"dependencies": {
"some-package": "github:github_username/some-package"
}
或者只是
"dependencies": {
"some-package": "github_username/some-package"
}
由于 Git 在底层使用
curl
,因此您可以使用带有凭据的 ~/.netrc
文件。对于 GitHub,它看起来像这样:
machine github.com
login <github username>
password <password OR github access token>
如果您选择使用
access tokens
,它可以从以下位置生成:
设置 -> 开发者设置 -> 个人访问令牌
如果您在自己的公司中使用 Github Enterprise,这也应该有效。只需将您的企业 github url 放入
machine
字段即可。
这里是如何使用 Github 令牌的更详细版本,无需在
package.json
文件中发布。
git config --global url."https://<TOKEN HERE>:[email protected]/".insteadOf https://[email protected]/
npm install --loglevel verbose --save git+https://[email protected]/<USERNAME HERE>/<REPOSITORY HERE>.git#v0.1.27
如果访问 Github 失败,请尝试运行
git ls-remote ...
命令,
此外,为了确保密钥的访问安全在 package.json 所在的同一目录级别创建 .env 文件。
“依赖关系”:{ ... "my-private-github-repo": "git+https://${ENV.PERSONAL_ACCESS_TOKEN}@github.com/USER/abcd-repo-3.4.0.git", ... }
还有其他使用“DOTENV”npm 包的方法,但是当我们尝试解决“Github”包依赖关系时,它没有多大作用。上面似乎是直接的解决方案。
还有
ssh-add ~/.ssh/id_rsa
请注意,您尝试添加为 package.json 文件的依赖项的 github 存储库需要定义自己的 package.json 文件。
对于我的私有存储库引用,我不想包含安全令牌,并且其他简单的(即仅在 package.json 中指定)都不起作用。 这是有效的:
访问 GitHub.com