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

吕自怡
2023-12-01

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

 类似资料: