简体   繁体   English

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

[英]npm install private github repositories by dependency in package.json

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

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 文档

The following worked just fine in all scenarios i needed :以下在我需要的所有场景中都运行良好:

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

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. commit-ish 可以是任何可以作为参数提供给 git checkout 的标签、sha 或分支。 The default is master.默认是主。

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 git@github.com:

GITHUB_TOKEN may 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-prebuild script in package.json and setup GITHUB_TOKEN as Heroku config variable.也适用于 Heroku,只需将上面的git config ...命令设置为package.json heroku-prebuild脚本,并将GITHUB_TOKEN设置为 Heroku 配置变量。

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]:x-oauth-basic@github.com/user/repo.git"

eg例如

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

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

Since Git uses curl under the hood, you can use ~/.netrc file with the credentials.由于 Git 在幕后使用curl ,您可以使用~/.netrc文件和凭据。 For GitHub it would look something like this:对于 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.如果您在自己的公司中使用 Github Enterprise,这也应该有效。 just put your enterprise github url in the machine field.只需将您的企业 github url 放在machine字段中。

Here is a more detailed version of how to use the Github token without publishing in the package.json file.这里有一个更详细的版本,说明如何使用 Github 令牌而不在package.json文件中发布。

  1. Create personal github access token创建个人 github 访问令牌
  2. Setup url rewrite in ~/.gitconfig在 ~/.gitconfig 中设置 url 重写
git config --global url."https://<TOKEN HERE>:x-oauth-basic@github.com/".insteadOf https://x-oauth-basic@github.com/
  1. Install private repository.安装私有仓库。 Verbose log level for debugging access errors.用于调试访问错误的详细日志级别。
npm install --loglevel verbose --save git+https://x-oauth-basic@github.com/<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 失败,请尝试运行npm install will printgit ls-remote ...命令

Further, in order to make key's access secure此外,为了使密钥的访问安全

  1. Create .env file at the same directory level where package.json resides.在 package.json 所在的同一目录级别创建 .env 文件。
  2. Mention PERSONAL_ACCESS_TOKEN=******************************* into .env file在 .env 文件中提及 PERSONAL_ACCESS_TOKEN=***********************************
  3. Dont forget to add '.env' into .gitingore list which will prevent exposing key to outside world while you make git commit to your repo.不要忘记将“.env”添加到 .gitingore 列表中,这将防止在您将 git 提交到您的存储库时将密钥暴露给外部世界。
  4. Now you can add your dependency in package.json as below,现在您可以在 package.json 中添加您的依赖项,如下所示,

Package.json包.json

"dependencies": { ... "my-private-github-repo": "git+https://${ENV.PERSONAL_ACCESS_TOKEN}@github.com/USER/abcd-repo-3.4.0.git", ... } "dependencies": { ... "my-private-github-repo": "git+https://${ENV.PERSONAL_ACCESS_TOKEN}@github.com/USER/abcd-repo-3.4.0.git", . .. }

There are other ways using 'DOTENV' npm package, but it could not do much when we are trying to resolve "Github" package dependency.还有其他使用“DOTENV”npm 包的方法,但是当我们尝试解决“Github”包依赖性时,它无能为力。 Above seems to be straight forward solution.以上似乎是直接的解决方案。

For my private repository reference I didn't want to include a secure token, and none of the other simple (ie specifying only in package.json) worked.对于我的私有存储库引用,我不想包含安全令牌,其他简单的(即仅在 package.json 中指定)都不起作用。 Here's what did work:这是有效的:

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

There's also SSH Key - Still asking for password and passphrase还有SSH 密钥 - 仍然要求输入密码和密码

Using ssh-add ~/.ssh/id_rsa without a local keychain.在没有本地钥匙串的情况下使用ssh-add ~/.ssh/id_rsa

This avoids having to mess with tokens.这避免了必须弄乱令牌。

If you want to add the dependency that is not anchored to master nor to a particular commit, you can do it by using semver.如果要添加既不锚定到 master 也不锚定到特定提交的依赖项,可以使用 semver 来完成。 Like that:像那样:

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

请注意,您尝试作为依赖项添加到 package.json 文件的 github 存储库需要定义自己的 package.json 文件。

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM