Node.js + npm(PATH 配置)

OpenClaw 需要 Node 22+ 版本。

如果你能运行 npm install -g openclaw@latest,但之后提示 openclaw: command not found,几乎都是 PATH 问题:npm 存放全局命令的目录不在你的 shell PATH 里。

快速诊断

运行:

node -v
npm -v
npm prefix -g
echo "$PATH"

如果 $(npm prefix -g)/bin(macOS/Linux)或 $(npm prefix -g)(Windows)不在 echo "$PATH" 的输出里,你的 shell 就找不到全局 npm 命令(包括 openclaw)。

修复:把 npm 全局命令目录加到 PATH

  1. 找到你的全局 npm 前缀:
npm prefix -g
  1. 把全局 npm bin 目录加到你的 shell 启动文件:
  • zsh:~/.zshrc
  • bash:~/.bashrc

示例(把路径替换成你的 npm prefix -g 输出):

# macOS / Linux
export PATH="/path/from/npm/prefix/bin:$PATH"

然后打开新终端(或在 zsh 里运行 rehash,bash 里运行 hash -r)。

Windows 上,把 npm prefix -g 的输出加到你的 PATH 环境变量。

修复:避免 sudo npm install -g / 权限错误(Linux)

如果 npm install -g ...EACCES 错误,把 npm 全局前缀改到用户可写的目录:

mkdir -p "$HOME/.npm-global"
npm config set prefix "$HOME/.npm-global"
export PATH="$HOME/.npm-global/bin:$PATH"

export PATH=... 这行加到你的 shell 启动文件里。

推荐的 Node 安装方式

如果 Node/npm 的安装方式满足以下条件,你会少遇到很多问题:

  • 保持 Node 更新(22+)
  • 全局 npm bin 目录稳定且在新 shell 的 PATH 里

常见选择:

  • macOS:Homebrew(brew install node)或版本管理器
  • Linux:你喜欢的版本管理器,或发行版支持的安装方式(提供 Node 22+)
  • Windows:官方 Node 安装包、winget 或 Windows Node 版本管理器

如果你用版本管理器(nvm/fnm/asdf 等),确保它在你日常使用的 shell(zsh 或 bash)里初始化了,这样运行安装程序时 PATH 才会正确设置。