运行安装claude-code失败,报错如下:

(base) yuanyh@ps:~$ npm i -g @anthropic-ai/claude-code
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE   package: '@anthropic-ai/claude-code@1.0.117',
npm WARN EBADENGINE   required: { node: '>=18.0.0' },
npm WARN EBADENGINE   current: { node: 'v12.22.9', npm: '8.5.1' }
npm WARN EBADENGINE }
npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /usr/local/lib/node_modules
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules'
npm ERR!  [Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'mkdir',
npm ERR!   path: '/usr/local/lib/node_modules'
npm ERR! }
npm ERR! 
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR! 
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/yuanyh/.npm/_logs/2025-09-18T08_36_17_771Z-debug-0.log

原因:

  1. Node.js 版本过低:@anthropic-ai/claude-code 需要 Node.js 18.0.0 或更高版本,而当前使用的是 v12.22.9。(具体看报错信息)
  2. 权限不足:没有足够的权限在 /usr/local/lib/node_modules 目录下创建文件。

解决方式

  1. 升级 Node.js 版本
    首先需要将 Node.js 升级到 18.0.0 或更高版本。可以使用 nvm(Node Version Manager)来管理 Node.js 版本:
# 安装 nvm(如果尚未安装)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash

# 重新加载终端或运行
source ~/.bashrc  # 或 ~/.zshrc,取决于你的 shell

# 安装并使用 Node.js 18
nvm install 18
nvm use 18

配置 npm 使用用户目录:

mkdir -p ~/.npm-global/lib ~/.npm-global/bin
npm config set prefix "/home/ps/.npm-global"
npm install -g @anthropic-ai/claude-code
npm list -g --depth=0
claude --version
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐