← 返回 CLI 配置教程

Claude Code 无法连接到 Anthropic 服务

注意事项:如果你是按 CLI 配置教程里的链接首次跳转到这一步,直接按下面的命令运行即可,跑完再回去继续 CLI 配置。

症状

用 npm 装完 claude 后,在命令行执行 claude,出现下面的错误:

text
Unable to connect to Anthropic services
Failed to connect to api.anthropic.com: ERR_BAD_REQUEST
Please check your internet connection and network settings.
Note: Claude Code might not be available in your country, check supported countries at https://anthropic.com/supported-countries

或者在初次配置时停在 Welcome to Claude Code,但选择登录方式后无法继续,本质都是 onboarding 状态没写入 ~/.claude.json。下面的命令直接补上这个字段。

Windows

  1. 按下键盘 Win + R,输入 cmd 后回车,打开命令行程序。
  2. 在命令行中粘贴下面的命令并回车:
bash
powershell -Command "$f='%USERPROFILE%.claude.json';$j=Get-Content $f|ConvertFrom-Json;$j|Add-Member -NotePropertyName 'hasCompletedOnboarding' -NotePropertyValue $true -Force;$j|ConvertTo-Json|Set-Content $f"

执行成功后重启 Claude CLI,再运行 claude 就能进入交互界面。

macOS

  1. 在应用程序中找到「终端」并打开。
  2. 在终端中粘贴下面的命令并回车:
bash
jq '. + {"hasCompletedOnboarding": true}' ~/.claude.json > /tmp/tmp.json && mv /tmp/tmp.json ~/.claude.json
提示:如果终端提示找不到 jq,先执行 brew install jq 安装,然后重新运行上面的命令。

执行成功后重启 Claude CLI,再运行 claude 就能进入交互界面。