Mac npm install时提示git@github.com:Permission denied (publickey)

Posted 不与天斗姜大夫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mac npm install时提示git@github.com:Permission denied (publickey)相关的知识,希望对你有一定的参考价值。

npm安装truffle-contract时报如下错误

% npm install truffle-contract --save                                  

npm ERR! code 128
npm ERR! An unknown git error occurred
npm ERR! command git --no-replace-objects ls-remote ssh://git@github.com/web3-js/WebSocket-Node.git
npm ERR! git@github.com: 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.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/domino/.npm/_logs/2023-01-25T09_49_32_140Z-debug-0.log

原因是没有配置 github的ssh key,没有权限通过ssh下载github上的项目。

解决方法:

  • 生成ssh key

    .ssh % ssh-keygen -t rsa -b 4096 -C "你的邮箱@163.com"
    Generating public/private rsa key pair.
    # 这里要输入rsa文件的位置,注意不要覆盖掉默认的,避免影响到已有的ssh连接
    Enter file in which to save the key (/Users/domino/.ssh/id_rsa): /Users/domino/.ssh/id_rsa_github
    # 直接回车到底
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in /Users/domino/.ssh/id_rsa_github.
    Your public key has been saved in /Users/domino/.ssh/id_rsa_github.pub.
    The key fingerprint is:
    SHA256:0qmF2i4vW05EYxIZJv1TI5wv1zRldtmh1GDLp5g9mM0 xxxx@163.com
    The key's randomart image is:
    ………………
    
  • 配置到 ~/.ssh/config文件中

    # 在 ~/.ssh/config文件中添加以下代码,如果没有则需要先创建
    
    #github
    Host *.github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_github
    

配置好重新执行安装命令,安装成功

% npm install truffle-contract --save
npm WARN deprecated uuid@2.0.1: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated uuid@2.0.1: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated uuid@2.0.1: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated uuid@3.3.2: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated truffle-interface-adapter@0.2.5: WARNING: This package has been renamed to @truffle/interface-adapter.
npm WARN deprecated uuid@3.3.2: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated truffle-contract@4.0.31: WARNING: This package has been renamed to @truffle/contract.

added 196 packages in 42s

参考文档:
https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
https://blog.csdn.net/huangsy421/article/details/121489441

以上是关于Mac npm install时提示git@github.com:Permission denied (publickey)的主要内容,如果未能解决你的问题,请参考以下文章

appium----已解决Mac安装sudo npm install -g appium-doctor总是提示“Err

使用Mac OS X 终端连接Git@Git.oschina.net

npm install 装包时提示Error EACCES permission denied解决办法

MAC配置git命令补全功能

MAC 在使用NPM时,提示权限不足的问题

Centos 7.3搭建git服务器