由于私人git仓库ssh和https连接,npm安装失败

时间:2019-01-03 05:37:47

标签: git npm ssh package.json

我有以下package.json文件,我正在尝试运行npm install,但失败。

    "test": "git+https://<TOKEN>@github.build.test.com/test.git",

我有与github匹配的ssh键。但是,即使我编写以下内容,它也始终与https一起运行。

    "test": "git+ssh://git@github.build.test.com/test.git",

在两种情况下,我都会遇到以下错误消息

npm ERR! Error while executing:
npm ERR! C:\Program Files\Git\cmd\git.EXE ls-remote -h -t https://github.build.test.com/test.git
npm ERR!
npm ERR! remote: Password authentication is not available for Git operations.
npm ERR! remote: You must use a personal access token or SSH key.
npm ERR! remote: See https://github.build.test.com/settings/tokens or https://github.build.test.com/settings/ssh
npm ERR! fatal: unable to access 'https://github.build.test.com/test.git/': The requested URL returned error: 403
npm ERR!
npm ERR! exited with error code: 128

如果我单独运行上述命令,它将起作用。

git ls-remote -h -t https://<TOKEN>@github.build.test.com/test.git
git ls-remote -h -t ssh://git@github.build.test.com/test.git

我该如何解决?

谢谢

2 个答案:

答案 0 :(得分:2)

首先,如果您的git配置中有任何url.<base>.insteadOf directive

cd /path/to/repo
git config -l|grep -i insteadOf

如果没有,请设置您自己的一个:

git config url."ssh://git@github.build.test.com".insteadOf  https://github.build.test.com

然后查看它是否仍在使用https。

答案 1 :(得分:0)

如果您要使用“ git + ssh://git@github.build.test.com/test.git”,则应该能够克隆此存储库而无需传递凭据。 简单的git clone git+ssh://git@github.build.test.com/test.git不应要求通过或用户名。

您可能需要配置git公钥/私钥。您本地用于克隆的密钥必须添加到存储库配置中。例如,可以在https://github.com/settings/keys

上配置github。