pm ERR! code 128 npm ERR! An unknown git error occurred npm ERR! command git --no-replace-objects ls-remote ssh://[email protected]/ant-design/plots.git npm ERR! Warning: Permanently added 'github.com' (ED25519) to the list of known hosts. npm ERR! [email protected]: 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.
时间: 2025-08-05 14:59:13 AIGC 浏览: 18
在使用 `npm install` 安装依赖时,如果遇到 `[email protected]: Permission denied (publickey)` 错误,通常是由于 Git 无法通过 SSH 认证访问远程仓库。以下是解决该问题的详细步骤:
### 生成 SSH 密钥
如果尚未生成 SSH 密钥,可以通过以下命令生成:
```bash
ssh-keygen -t rsa -b 4096 -C "[email protected]"
```
按提示操作,通常可以直接按回车键使用默认路径和空密码。生成的密钥将保存在 `~/.ssh/id_rsa` 和 `~/.ssh/id_rsa.pub` 中[^2]。
### 添加 SSH 密钥到 GitHub
将生成的公钥(`~/.ssh/id_rsa.pub`)内容复制到 GitHub 账户中:
1. 打开终端并运行以下命令以显示公钥:
```bash
cat ~/.ssh/id_rsa.pub
```
2. 登录 GitHub,进入 **Settings > SSH and GPG keys**,点击 **New SSH key**,将公钥粘贴进去并保存[^2]。
### 测试 SSH 连接
运行以下命令测试与 GitHub 的连接:
```bash
ssh -T [email protected]
```
如果一切正常,会看到欢迎信息,表明已成功连接到 GitHub[^2]。
### 配置 Git 用户信息
确保 Git 用户信息配置正确:
```bash
git config --global user.name "YourName"
git config --global user.email "[email protected]"
```
这一步有助于避免因用户信息不匹配导致的权限问题[^5]。
### 使用 HTTPS 替代 SSH
如果不想使用 SSH,可以尝试将依赖项的 Git URL 从 SSH 格式改为 HTTPS 格式。例如,将 `[email protected]:username/repo.git` 替换为 `https://github.com/username/repo.git`。可以通过以下命令修改 Git 的全局配置:
```bash
git config --global url."https://github.com/".insteadOf [email protected]:
```
这样,Git 会自动将 SSH URL 替换为 HTTPS URL,避免 SSH 权限问题[^3]。
### 检查 SSH 配置文件
如果上述方法无效,可以检查 `~/.ssh/config` 文件,确保没有错误的配置干扰 SSH 连接。可以尝试创建一个简单的配置文件,确保只包含必要的设置:
```bash
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
```
保存后,重新测试 SSH 连接。
### 清除 Git 缓存
有时,Git 缓存可能导致权限问题。可以尝试清除缓存并重新尝试安装依赖:
```bash
git credential-cache exit
```
然后重新运行 `npm install` 命令[^3]。
通过以上步骤,应该能够解决 `npm ERR! [email protected]: Permission denied (publickey)` 错误,并顺利完成依赖安装。
阅读全文