npm ERR! code 128 解决GitHub SSH权限问题

发布时间 2023-11-29 11:01:44作者: 纯真丁一郎です

报错原因

在npm项目部署时,运行npm install时报错如下:

PS D:\workspace\web\vue-element-admin> npm install
npm ERR! code 128
npm ERR! An unknown git error occurred
npm ERR! command git --no-replace-objects ls-remote ssh://git@github.com/nhn/raphael.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报错 code 128

并且致命原因是因为没有远程仓库的权限
通过查看上面两个ERR报错,可以看到通过ssh拉取git仓库

npm ERR! command git --no-replace-objects ls-remote ssh://git@github.com/nhn/raphael.git
npm ERR! git@github.com: Permission denied (publickey).
npm ERR! fatal: Could not read from remote repository.

因为这里执行的 ls-remote ssh://git@github.com/nhn/raphael.git没有可用的主机验证秘钥

解决方案

解决 GitHub SSH key 问题,详细介绍可参考:

https://www.cnblogs.com/cenima/p/17864044.html

简单步骤如下:

  1. 输入 ls ~/.ssh 来查看 SSH keys 是否存在.
$ ls -al ~/.ssh
# Lists the files in your .ssh directory, if they exist
  1. 使用 ssh-keygen 命令生成密钥,其中邮箱改为你自己的
ssh-keygen -t ed25519 -C "your_email@example.com"
  1. 复制 SSH public key 内容,在 GitHub 网站右上角个人头像Settings 页面选择 SSH and GPG keys 项, 点击 New SSH key 按钮添加, 在 Key 字段粘贴刚才复制的公钥内容