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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23210437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:11:05  来源:igfitidea点击:

npm install private github repositories by dependency in package.json

node.jsgithubdependenciesnpmpackage.json

提问by vashishatashu

I'm trying to install github private repository by npm that includes other private github repositories as dependency.

我正在尝试通过 npm 安装 github 私有存储库,其中包含其他私有 github 存储库作为依赖项。

Have tried a lot of ways and posts but none is working. Here is what i'm doing :

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

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

in package.json is like :

在 package.json 中是这样的:

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

What is the the right way to do it?

正确的做法是什么?

采纳答案by vashishatashu

The following worked just fine in all scenarios i needed :

以下在我需要的所有场景中都运行良好:

"dependencies": {
"GitRepo": "git+https://<token-from-github>:[email protected]/<user>/<GitRepo>.git"
}

回答by leko

Try this:

尝试这个:

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

You could also try this, where visionmedia/express is name/repo:

你也可以试试这个,其中 visionmedia/express 是名称/仓库:

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

Or (if the npm package module exists):

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

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

Taken from NPM docs

取自NPM 文档

回答by mummybot

For those of you who came here for public directories, from the npm docs: https://docs.npmjs.com/files/package.json#git-urls-as-dependencies

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

Git URLs as Dependencies

Git URL 作为依赖项

Git urls can be of the form:

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

The commit-ish can be any tag, sha, or branch which can be supplied as an argument to git checkout. The default is master.

commit-ish 可以是任何可以作为参数提供给 git checkout 的标签、sha 或分支。默认是主。

回答by Michal Moravcik

The accepted answer works, but I don't like much the idea to paste secure tokens into the package.json

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

I have found it elsewhere, just run this one-time command as documented in the git-config manpage.

我在其他地方找到了它,只需按照 git-config 手册页中的说明运行这个一次性命令。

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

GITHUB_TOKENmay be setup as environmnet variable or pasted directly

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

and then I install private github repos like: npm install user/repo --save

然后我安装私有 github 存储库,如: npm install user/repo --save



works also in Heroku, just setup the above git config ...command as heroku-prebuildscript in package.jsonand setup GITHUB_TOKENas Heroku config variable.

也适用于 Heroku,只需将上述git config ...命令设置为heroku-prebuild脚本package.json并设置GITHUB_TOKEN为 Heroku 配置变量。

回答by Adrian

There are multiple ways to do it as people point out, but the shortest versions are:

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

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

e.g.

例如

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

回答by Rayron Victor

"dependencies": {
  "some-package": "github:github_username/some-package"
}

or just

要不就

"dependencies": {
  "some-package": "github_username/some-package"
}

https://docs.npmjs.com/files/package.json#github-urls

https://docs.npmjs.com/files/package.json#github-urls

回答by LeOn - Han Li

Since Git uses curlunder the hood, you can use ~/.netrcfile with the credentials. For GitHub it would look something like this:

由于 Gitcurl在幕后使用,您可以使用~/.netrc带有凭据的文件。对于 GitHub,它看起来像这样:

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

If you choose to use access tokens, it can be generated from:

如果您选择使用access tokens,则可以从以下位置生成:

Settings -> Developer settings -> Personal access tokens

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

This should also work if you are using Github Enterprise in your own corporation. just put your enterprise github url in the machinefield.

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

回答by Martins Balodis

Here is a more detailed version of how to use the Github token without publishing in the package.jsonfile.

这里有一个更详细的版本,说明如何在不发布package.json文件的情况下使用 Github 令牌。

  1. Create personal github access token
  2. Setup url rewrite in ~/.gitconfig
  1. 创建个人 github 访问令牌
  2. 在 ~/.gitconfig 中设置 url 重写
git config --global url."https://<TOKEN HERE>:[email protected]/".insteadOf https://[email protected]/
  1. Install private repository. Verbose log level for debugging access errors.
  1. 安装私有仓库。用于调试访问错误的详细日志级别。
npm install --loglevel verbose --save git+https://[email protected]/<USERNAME HERE>/<REPOSITORY HERE>.git#v0.1.27

In case access to Github fails, try running the git ls-remote ...command that the npm install will print

如果访问 Github 失败,请尝试运行以下git ls-remote ...命令npm install will print

回答by Flint Weather

There's also SSH Key - Still asking for password and passphrase

还有SSH 密钥 - 仍然要求输入密码和密码

Using ssh-add ~/.ssh/id_rsawithout a local keychain.

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

This avoids having to mess with tokens.

这避免了必须弄乱令牌。

回答by The Coder

For my private repository reference I didn't want to include a secure token, and none of the other simple (i.e. specifying only in package.json) worked. Here's what did work:

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

  1. Went to GitHub.com
  2. Navigated to Private Repository
  3. Clicked "Clone or Download" and Copied URL (which didn't match the examples above)
  4. Added #commit-sha
  5. Ran npm install
  1. 去了 GitHub.com
  2. 导航到私有存储库
  3. 单击“克隆或下载”并复制 URL(与上面的示例不匹配)
  4. 添加#commit-sha
  5. 运行 npm 安装