npm通过package.json中的依赖项安装私有github存储库

node.js Node.js

Eva西门

2020-03-23

我正在尝试通过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"
}

正确的方法是什么?

第2913篇《npm通过package.json中的依赖项安装私有github存储库》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

10个回答
老丝阳光 2020.03.23

还有SSH密钥-仍在询问密码和密码

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

这避免了必须弄乱令牌。

Tony宝儿 2020.03.23

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

  1. 创建个人github访问令牌
  2. 在〜/ .gitconfig中重写设置URL
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

乐米亚 2020.03.23

由于Git是curl后台使用的,因此可以使用~/.netrc带有凭据的文件。对于GitHub,它看起来像这样:

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

如果选择使用access tokens,则可以通过以下方式生成:

设置->开发人员设置->个人访问令牌

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

泡芙 2020.03.23

对于我的私有存储库参考,我不想包含安全令牌,并且其他任何简单方法(即仅在package.json中指定)都无效。这是起作用的:

  1. 去了GitHub.com
  2. 导航到私有存储库
  3. 点击“克隆或下载”和复制的网址(与上面的示例不匹配)
  4. 添加了#commit-sha
  5. 跑npm安装
西门飞云 2020.03.23

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

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

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

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

例如

"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",
}
猿小宇宙小哥 2020.03.23

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

我在其他地方找到了它,只需运行git-config联机帮助页中记录的一次性命令即可

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

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

然后我安装像这样的私有github仓库: npm install user/repo --save


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

阿飞 2020.03.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

阿飞 2020.03.23

以下在我需要的所有情况下都可以正常工作:

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

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

Git URL作为依赖项

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。

番长Itachi 2020.03.23

尝试这个:

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

您还可以尝试以下操作,其中visionmedia / express是名称/存储库:

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

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

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

取自NPM文档

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android