![](/img/trans.png)
[英]npm package from private github repo, install vs update (package.json)
[英]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 網址可以采用以下形式:
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"
}
由於 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
文件中發布。
git config --global url."https://<TOKEN HERE>:x-oauth-basic@github.com/".insteadOf https://x-oauth-basic@github.com/
npm install --loglevel verbose --save git+https://x-oauth-basic@github.com/<USERNAME HERE>/<REPOSITORY HERE>.git#v0.1.27
如果訪問 Github 失敗,請嘗試運行npm install will print
的git ls-remote ...
命令
此外,為了使密鑰的訪問安全
包.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 中指定)都不起作用。 這是有效的:
如果要添加既不錨定到 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.