我试图通过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"
}

正确的做法是什么?


当前回答

如果您想添加既不锚定到master也不锚定到特定提交的依赖项,可以使用semver来完成。像这样:

"dependencies": {
  "some-package": "github:github_username/some-package#semver:^1.0.0"
}

其他回答

对于那些来这里获取公共目录的人,请访问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。

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

// 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"

如。

"dependencies" : {
  "hexo-renderer-marked": "amejiarosario/dsa.jsd#book",
  "hexo-renderer-marked": "amejiarosario/dsa.js#8ea61ce",
  "hexo-renderer-marked": "amejiarosario/dsa.js",
}

还有SSH密钥-仍然要求密码和密码短语

使用ssh-add ~/。Ssh /id_rsa没有本地keychain。

这样就避免了与令牌纠缠不清。

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

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

因为Git在底层使用curl,所以可以使用~/.netrc文件作为凭证。对于GitHub,它看起来像这样:

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

如果你选择使用访问令牌,它可以从:

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

如果你在自己的公司使用Github企业,这也应该有效。只要把你的企业github网址放在机器字段。