簡體   English   中英

"npm 通過 package.json 中的依賴項安裝私有 github 存儲庫"

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

我正在嘗試通過 npm 安裝 github 私有存儲庫,其中包括其他私有 github 存儲庫作為依賴項。

嘗試了很多方法和帖子,但沒有一個有效。 這是我正在做的事情:

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

嘗試這個:

"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 文檔

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

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

對於那些來到這里獲取公共目錄的人,來自 npm 文檔: 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 可以是任何可以作為參數提供給 git checkout 的標簽、sha 或分支。 默認是主。

接受的答案有效,但我不太喜歡將安全令牌粘貼到package.json的想法

我在其他地方找到了它,只需按照 git-config 手冊頁中的說明運行這個一次性命令。

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

GITHUB_TOKEN可以設置為環境變量或直接粘貼

然后我安裝私有 github 存儲庫,如: npm install user/repo --save


也適用於 Heroku,只需將上面的git config ...命令設置為package.json heroku-prebuild腳本,並將GITHUB_TOKEN設置為 Heroku 配置變量。

正如人們指出的那樣,有多種方法可以做到,但最短的版本是:

// 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",
}
"dependencies": {
  "some-package": "github:github_username/some-package"
}

要不就

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

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

由於 Git 在幕后使用curl ,您可以使用~/.netrc文件和憑據。 對於 GitHub,它看起來像這樣:

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

如果您選擇使用access tokens ,則可以從以下位置生成:

設置 -> 開發者設置 -> 個人訪問令牌

如果您在自己的公司中使用 Github Enterprise,這也應該有效。 只需將您的企業 github url 放在machine字段中。

這里有一個更詳細的版本,說明如何使用 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 失敗,請嘗試運行npm install will printgit ls-remote ...命令

此外,為了使密鑰的訪問安全

  1. 在 package.json 所在的同一目錄級別創建 .env 文件。
  2. 在 .env 文件中提及 PERSONAL_ACCESS_TOKEN=***********************************
  3. 不要忘記將“.env”添加到 .gitingore 列表中,這將防止在您將 git 提交到您的存儲庫時將密鑰暴露給外部世界。
  4. 現在您可以在 package.json 中添加您的依賴項,如下所示,

包.json

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

還有其他使用“DOTENV”npm 包的方法,但是當我們嘗試解決“Github”包依賴性時,它無能為力。 以上似乎是直接的解決方案。

對於我的私有存儲庫引用,我不想包含安全令牌,其他簡單的(即僅在 package.json 中指定)都不起作用。 這是有效的:

  1. 去了 GitHub.com
  2. 導航到私有存儲庫
  3. 單擊“克隆或下載”並復制 URL(與上面的示例不匹配)
  4. 添加#commit-sha
  5. 運行 npm 安裝

還有SSH 密鑰 - 仍然要求輸入密碼和密碼

在沒有本地鑰匙串的情況下使用ssh-add ~/.ssh/id_rsa

這避免了必須弄亂令牌。

如果要添加既不錨定到 master 也不錨定到特定提交的依賴項,可以使用 semver 來完成。 像那樣:

"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