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

我试图安装一个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 install。 我认为问题是,当我作为sudo运行时,它正在/var/root/.ssh中查找关键字。 我没有root用户凭据,否则我会创build一个新的密钥作为根。 还有其他的工作吗?

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

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

我认为问题是,当我作为sudo运行时,它正在/var/root/.ssh中查找关键字。 我没有root用户凭据

解决方法是将该回购网站的URL更改为http:它将查询您的login名/密码,而不是依赖不能以root身份提供的ssh密钥。

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

如果我改变git url来使用http,它可以正常工作,但是这样做并不是一个好主意,因为当我将包检查回到repo时,我不得不改变它。

其实,你可以改变一个url而不用修改远程url的configuration。
例如(如在如何使用SSH而不是HTTP的Git子模块? )

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