本文整理 npm 和 PowerShell 独立安装两种方式。除 .npmrc 配置内容外,以下命令均可在 PowerShell 中执行。
国内网络环境下,配置了可访问的国内镜像或企业内部 npm 源后,npm 安装通常更方便。PowerShell 独立安装需要访问官方脚本和程序下载服务,部分网络可能出现连接失败、下载缓慢,或被企业网络策略拦截。
实际速度取决于网络、代理及镜像服务状态;国内镜像也可能存在版本同步延迟。
1.通过 npm 安装与更新
使用前,需要安装 Node.js 和 npm。可以先检查:
node --version
npm.cmd --version
查询可用版本
查看所有已发布版本:
# Codex CLI
npm.cmd view @openai/codex versions --json
# Claude Code CLI
npm.cmd view @anthropic-ai/claude-code versions --json
只查看 latest 标签对应的版本:
npm.cmd view @openai/codex@latest version
npm.cmd view @anthropic-ai/claude-code@latest version
这些命令查询当前配置的 npm 源。如果使用镜像源,结果可能与官方源存在同步时间差。
安装或更新到最新版
首次安装和更新使用相同命令:
# Codex CLI
npm.cmd install -g @openai/codex@latest --registry=https://registry.npmmirror.com/
--verbose 时会有进度
npm.cmd install -g @openai/codex@latest --verbose
# Claude Code CLI
npm.cmd install -g @anthropic-ai/claude-code@latest --allow-scripts=@anthropic-ai/claude-code --foreground-scripts --registry=https://registry.npmmirror.com/
npm.cmd install -g @anthropic-ai/claude-code@latest --allow-scripts=@anthropic-ai/claude-code --foreground-scripts --registry=https://registry.npmmirror.com/ --verbose
安装指定版本
将 latest 替换为目标版本号。例如:
npm.cmd install -g @openai/codex@0.162.1
npm.cmd install -g @anthropic-ai/claude-code@2.1.238
以上版本号仅作为示例,实际以版本查询结果为准。安装和更新方式参考:Codex 官方说明、Claude 官方说明。
Claude 安装脚本被阻止时的处理
如果当前 npm 版本要求显式允许安装脚本,可使用:
npm.cmd install -g @anthropic-ai/claude-code@latest --allow-scripts=@anthropic-ai/claude-code --foreground-scripts
| 参数 | 作用 |
|---|---|
--allow-scripts=@anthropic-ai/claude-code |
允许该包执行安装脚本 |
--foreground-scripts |
在终端中显示安装脚本输出,便于排查问题 |
这是此前远程机器安装 Claude 时遇到的问题对应的处理方式;旧版 npm 可能不支持 --allow-scripts,应结合当前版本和报错使用。npm 安装参数说明
2.通过 PowerShell 独立安装与更新
这种方式直接使用官方安装脚本,不需要通过 npm 安装。
Codex CLI 安装
powershell -ExecutionPolicy Bypass -c "irm https://chatgpt.com/codex/install.ps1 | iex"
Claude Code CLI 安装
irm https://claude.ai/install.ps1 | iex
安装完成后,新开终端,再检查版本。
手动更新
| 软件 | 更新命令 |
|---|---|
| Codex CLI | codex update |
| Claude Code CLI | claude update |
codex update 要求已安装版本支持自更新,可以先执行 codex update --help 核对;不支持时,可重新运行官方安装脚本。Claude 原生安装默认支持后台自动更新,但会受更新配置和环境变量影响。Codex 更新说明、Claude 更新说明
npm 安装的版本优先使用 npm 命令更新,独立安装的版本使用对应的原生更新方式。
3.查看已安装版本和实际命令入口
版本查询统一使用 两个短横线的 --version:
codex --version
claude --version
如果同时安装了多套,先确认命令实际指向哪里:
Get-Command codex,claude -All
where.exe codex
where.exe claude
也可以通过完整路径,分别检查不同安装方式的版本。以下适用于默认安装目录:
# npm 安装版
& "$env:APPDATA\npm\codex.cmd" --version
& "$env:APPDATA\npm\claude.cmd" --version
# PowerShell 独立安装版
& "$env:LOCALAPPDATA\Programs\OpenAI\Codex\bin\codex.exe" --version
& "$env:USERPROFILE\.local\bin\claude.exe" --version
如果自定义了 npm 安装位置,可通过以下命令核对:
npm.cmd prefix -g
做沙箱更新测试时,箱内和箱外应使用同一个完整入口查询版本,避免更新的是 npm 版,检查的却是独立安装版。
4.国内网络环境下的 npm 下载源配置
npm 默认使用官方源。可以根据实际网络情况,选择国内镜像或企业内部源。
| 下载源 | Registry 地址 | 使用说明 |
|---|---|---|
| npm 官方源 | https://registry.npmjs.org/ |
用于直接查询和下载官方发布的包 |
| npmmirror(原淘宝 npm 镜像) | https://registry.npmmirror.com/ |
国内镜像,可用于包查询和安装 |
| 腾讯云 npm 镜像 | https://mirrors.cloud.tencent.com/npm/ |
国内镜像,可按实际网络情况选择 |
地址依据:npmmirror 使用说明、腾讯云镜像配置说明。镜像的速度、可用性和版本同步情况,以使用时的实际结果为准。
方式一:设置当前用户的默认源
下面两种镜像选择一种即可。
使用 npmmirror:
npm.cmd config set registry https://registry.npmmirror.com/ --location=user
或者使用腾讯云镜像:
npm.cmd config set registry https://mirrors.cloud.tencent.com/npm/ --location=user
检查当前生效的源:
npm.cmd config get registry
恢复官方源:
npm.cmd config set registry https://registry.npmjs.org/ --location=user
这里明确使用 --location=user,修改的是当前用户的 npm 配置,通常保存在用户目录的 .npmrc 中,并不等于给计算机上的所有用户统一设置 。npm 配置范围说明
方式二:单次命令临时指定源
如果不想修改默认配置,可以在查询或安装命令中追加 --registry:
# 从指定镜像查询 Codex 版本
npm.cmd view @openai/codex versions --json --registry=https://registry.npmmirror.com/
# 从指定镜像安装或更新 Codex
npm.cmd install -g @openai/codex@latest --registry=https://registry.npmmirror.com/
# 从指定镜像安装或更新 Claude
npm.cmd install -g @anthropic-ai/claude-code@latest --registry=https://registry.npmmirror.com/
如果镜像暂时查不到目标版本,可指定官方源对照查询:
npm.cmd view @openai/codex@latest version --registry=https://registry.npmjs.org/
npm.cmd view @anthropic-ai/claude-code@latest version --registry=https://registry.npmjs.org/
方式三:使用 nrm 管理多个源
需要经常切换镜像时,可以安装 nrm:
npm.cmd install -g nrm --registry=https://registry.npmmirror.com/
常用命令:
# 查看可选源及其地址
nrm.cmd ls
# 测试各源的响应时间
nrm.cmd test
# 切换到 npmmirror,名称通常为 taobao
nrm.cmd use taobao
# 恢复官方源
nrm.cmd use npm
# 核对最终生效地址
npm.cmd config get registry
源名称和地址以 nrm.cmd ls 的实际输出为准。测速结果反映请求响应时间,可作为选择参考,不能直接代表大体积安装包的下载速度。nrm 使用说明
方式四:为团队项目配置 .npmrc
如果需要统一某个项目的依赖下载源,可在项目根目录创建 .npmrc,写入:
registry=https://registry.npmmirror.com/
团队成员在该项目中执行普通 npm install 时,会读取项目配置。
项目级 .npmrc 不适用于 npm install -g 全局安装。 安装 Codex CLI、Claude Code CLI 这类全局工具时,应使用用户级配置,或在命令中明确指定 --registry。npm 配置文件说明
npm 镜像设置只影响 npm 下载,不会改变 PowerShell 官方安装脚本的下载地址,也不会改变 Codex、Claude 调用模型时的服务地址。