运行npm install时,Git auth失败为sudo

时间:2016-01-29 03:13:32

标签: node.js git npm

我正在尝试安装一个来自我们的私人仓库的npm软件包。当我以自己的身份运行npm install时,我得到Please try running this command again as root/Administrator.当我以sudo运行它时,我得到了

npm ERR! Error: Command failed: Permission denied (publickey).
npm ERR! fatal: Could not read from remote repository.
npm ERR!
npm ERR! Please make sure you have the correct access rights
npm ERR! and the repository exists.

我通常不使用sudo来进行npm安装。我认为问题在于,当我以sudo身份运行时,它会在/var/root/.ssh中查找密钥。我没有root用户凭据,否则我将以root身份创建一个新密钥。还有另外的工作吗?

编辑:尝试了下面的建议,他们没有工作。但是,问题中的repo由包根下面的依赖项引用。 `./node_modules//package.json。如果我改变git url使用http,它可以工作,但这样做并不是一个好主意,因为当我将包检回到repo时我将不得不改变它。

2 个答案:

答案 0 :(得分:0)

  

我认为问题在于,当我以sudo身份运行时,它会在/var/root/.ssh中查找密钥。我没有root用户凭据

解决方法是将该repo的url更改为http:然后它将查询您的登录名/密码,而不是依赖于您无法以root身份提供的ssh密钥。

cd /path/to/private/repo
git remote set-url origin https://server/user/repo
  

如果我更改git url以使用http,它会起作用,但这样做并不是一个好主意,因为当我将包检回到repo时我将不得不更改它。

实际上,您可以通过另一个更改网址,而无需修改远程网址的配置。
例如(如How to use SSH instead of HTTP for Git submodules?

git config url.https://github.com/.insteadOf ssh://git@github.com/
# or
git config url.https://github.com/.insteadOf git@github.com:

答案 1 :(得分:0)

试试这个: sudo /bin/bash然后,运行你的npm命令