Node.js npm通过package.json中的依赖项安装私有github存储库
我正试图通过npm安装github私有存储库,其中包含其他私有github存储库作为依赖项 已经尝试了很多方法和帖子,但都没有奏效。以下是我正在做的:Node.js npm通过package.json中的依赖项安装私有github存储库,node.js,github,dependencies,npm,package.json,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
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"
}
您也可以尝试这样做,其中visionmedia/express是name/repo:
"dependencies" : {
"express" : "visionmedia/express"
}
或者(如果npm包模块存在):
摘自以下内容适用于我需要的所有场景:
"dependencies": {
"GitRepo": "git+https://<token-from-github>:x-oauth-basic@github.com/<user>/<GitRepo>.git"
}
“依赖项”:{
“GitRepo”:“git+https://:x-oauth-basic@github.com//.git“
}
对于来这里查看公共目录的人,请参阅npm文档:
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
提交ish可以是任何标记、sha或分支,它们可以作为git checkout的参数提供。默认值为master。正如人们指出的,有多种方法可以做到这一点,但最短的版本是:
// from master
"depName": "user/repo",
// specific branch
"depName": "user/repo#branch",
// specific commit
"depName": "user/repo#commit",
// private repo
"depName": "git+https://[TOKEN]:x-oauth-basic@github.com/user/repo.git"
e、 g
公认的答案是可行的,但我不太喜欢将安全令牌粘贴到
package.json
我在别处找到了它,只需运行这个一次性命令
GITHUB\u令牌
可以设置为环境变量或直接粘贴
然后我安装私有github repo,比如:npm install user/repo--save
同样适用于Heroku,只需将上面的
git config…
命令设置为package.json中的Heroku prebuild
脚本,并将GITHUB\u TOKEN
设置为Heroku config变量。对于我的私有存储库引用,我不想包含安全令牌,其他简单的设置都不包含(即,仅在package.json中指定)有效。以下是有效的方法:
访问GitHub.com
导航到专用存储库
单击“克隆或下载”并复制URL(与上面的示例不匹配)
添加了#提交sha
运行npm安装
或者只是
"dependencies": {
"some-package": "github_username/some-package"
}
由于Git在引擎盖下使用了curl
,因此您可以使用带有凭据的~/.netrc
文件。对于GitHub,它看起来像这样:
machine github.com
login <github username>
password <password OR github access token>
machine github.com
登录
密码
如果您选择使用访问令牌
,则可通过以下方式生成:
设置->开发者设置->个人访问令牌
如果您在自己的公司中使用Github Enterprise,这也应该起作用。只需将您的企业Github url放在机器
字段中。以下是如何使用Github令牌而不在包.json
文件中发布的更详细版本
创建个人github访问令牌
在~/.gitconfig中设置url重写
git-config——全局url。“https://:x-oauth-basic@github.com/“.代替https://x-oauth-basic@github.com/
安装专用存储库。调试访问错误的详细日志级别
npm安装--loglevelverbose--save git+https://x-oauth-basic@github.com/.git#v0.1.27
如果访问Github失败,请尝试运行git ls remote…
命令,该命令将由npm安装打印
使用ssh add~/.ssh/id\u rsa
而不使用本地密钥链
这避免了与令牌混淆。此外,为了确保密钥的访问安全
在package.json所在的同一目录级别创建.env文件
在.env文件中提及个人访问令牌=**************************************
别忘了将“.env”添加到.gitingore列表中,这样可以防止在git提交回购时将密钥暴露给外部世界
现在您可以在package.json中添加依赖项,如下所示
Package.json
“依赖项”:{
...
“我的私人github repo”:“git+https://${ENV.PERSONAL_ACCESS_TOKEN}@github.com/USER/abcd-repo-3.4.0.git”,
...
}
使用“DOTENV”npm包还有其他方法,但当我们试图解决“Github”包依赖关系时,它无法起到多大作用。上面的解决方案似乎是直截了当的。npm ERR!无法解决git HEAD()致命:模棱两可的参数“提交ish”:未知修订或路径不在工作树中。不起作用。不需要#xxxx,因为它被认为是主文件,我的所有工作都在主文件中。还有其他想法吗?如果删除标题(#提交ish),结果相同?您的存储库的名称是什么?@vashishatashu,关于fatal:不明确的参数“commit ish”:未知的修订或路径不在工作树中。
很明显,commit ish
只是一个示例词,您应该用您需要的特定提交的sha
替换它。(如果您需要主分支中的最新提交,请删除该#commit ish
)git+https://:x-oauth-basic@github.com//.git它支持身份验证,在任何情况下都能正常工作。你有这个解决方案的参考链接吗?@Ian:我在某处读过,但没有参考链接。我在生产中使用它一年多了,没有任何问题。你可以从Github获得oauth令牌:设置->应用程序->个人Access Token->生成新令牌。根据您的使用情况,此令牌可以具有读/写权限,也可以同时具有读/写权限。@lan:对于bitbucket,您可以使用以下语法:git clone https://:x-oauth-basic@bitbucket.org//可从以下位置获取:团队->管理团队->APIKey@vashishatashu,谢谢你的好主意。不过,我收到了“致命的:找不到布拉布拉赫”尝试此操作时,即使我已授予相关用户对回购的访问权。有什么想法吗?这里有一篇关于此技术的好文章:这也适用于Microsoft Visual Studio Team Services构建链,我在其中将其作为“命令”执行就在npm安装
步骤之前。太棒了!非常适合CI环境的解决方案!我确实需要对其进行一些修改;我必须将结尾更改为
git config --global url."https://${GITHUB_TOKEN}@github.com/".insteadOf git@github.com:
"dependencies": {
"some-package": "github:github_username/some-package"
}
"dependencies": {
"some-package": "github_username/some-package"
}
machine github.com
login <github username>
password <password OR github access token>
git config --global url."https://<TOKEN HERE>:x-oauth-basic@github.com/".insteadOf https://x-oauth-basic@github.com/
npm install --loglevel verbose --save git+https://x-oauth-basic@github.com/<USERNAME HERE>/<REPOSITORY HERE>.git#v0.1.27