我正在尝试通过 npm 安装 github 私有存储库,其中包括其他私有 github 存储库作为依赖项。
尝试了很多方法和帖子,但没有一个有效。这是我正在做的事情:
npm install git+https://github.com/myusername/mygitrepository.git
在 package.json 中是这样的:
"dependencies": {
"repository1name": "git+https://github.com/myusername/repository1.git",
"repository2name": "git+https://github.com/myusername/repository2.git"
}
正确的方法是什么?
尝试这个:
"dependencies" : {
"name1" : "git://github.com/user/project.git#commit-ish",
"name2" : "git://github.com/user/project.git#commit-ish"
}
你也可以试试这个,其中 visionmedia/express 是 name/repo:
"dependencies" : {
"express" : "visionmedia/express"
}
或者(如果存在 npm 包模块):
"dependencies" : {
"name": "*"
}
取自 NPM docs
以下在我需要的所有情况下都可以正常工作:
"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 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 可以是任何可以作为参数提供给 git checkout 的标记、sha 或分支。默认是主。
As of version 1.1.65, you can refer to GitHub urls as just “foo”: “user/foo-project”.
npmjs docs reference
git+https://
是什么意思?使用 git
协议进行克隆并在通过 https
时提取新更改?
接受的答案有效,但我不太喜欢将安全令牌粘贴到 package.json
我在别处找到了它,只需运行这个一次性命令 as documented in the git-config manpage。
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 配置变量。
npm install
步骤之前将其作为“命令”执行。
...insteadOf ssh://git@github.com
npm install user/repo --save
时它仍在使用 ssh。有什么我需要配置的吗?
正如人们指出的那样,有多种方法可以做到这一点,但最短的版本是:
// 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
字段中。
machine github.com login <token>
在一条线上和"package": "https://github.com/acme/privaterepo.git#commit-ish"
一起为我工作或直接与npm install https://github.com/acme/privaterepo.git#commit-ish
一起工作
以下是如何使用 Github 令牌而不在 package.json
文件中发布的更详细版本。
在 ~/.gitconfig 中创建个人 github 访问令牌 Setup url rewrite
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
如果要添加既不锚定到 master 也不锚定到特定提交的依赖项,可以使用 semver 来完成。像那样:
"dependencies": {
"some-package": "github:github_username/some-package#semver:^1.0.0"
}
对于我的私有存储库参考,我不想包含安全令牌,并且其他简单的(即仅在 package.json 中指定)都不起作用。这是有效的:
转到 GitHub.com 导航到私有存储库 单击“克隆或下载”并复制 URL(与上面的示例不匹配) 添加了 #commit-sha Ran npm install
此外,为了使密钥的访问安全
在 package.json 所在的同一目录级别创建 .env 文件。在 .env 文件中提及 PERSONAL_ACCESS_TOKEN=************************************* 不要忘记将 '.env' 添加到 .gitingore 列表中这将防止在您将 git 提交到您的仓库时将密钥暴露给外界。现在您可以在 package.json 中添加您的依赖项,如下所示,
包.json
“依赖项”:{ ... “我的私有 github-repo”:“git+https://${ENV.PERSONAL_ACCESS_TOKEN}@github.com/USER/abcd-repo-3.4.0.git”,。 .. }
还有其他使用 'DOTENV' npm 包的方法,但是当我们试图解决“Github”包依赖时它不能做太多事情。以上似乎是直截了当的解决方案。
请注意,您尝试作为依赖项添加到 package.json 文件的 github 存储库需要定义自己的 package.json 文件。
不定期副业成功案例分享
fatal: ambiguous argument 'commit-ish': unknown revision or path not in the working tree.
很明显,commit-ish
只是一个示例词,您应该将其替换为您需要的特定提交的sha
。 (或者如果您需要 master 分支中的最新提交,则完全删除该#commit-ish
)