Codex CLI 与 Claude Code CLI:Windows 安装、版本查询、更新及 npm 镜像配置

本文整理 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 调用模型时的服务地址。

相关推荐
枫叶丹45 小时前
AGENTS.md 越写越长之后:如何管理 AI 的上下文
人工智能·chatgpt·开源·agent·codex
坐吃山猪6 小时前
Codex使用说明
人工智能·开发·codex
一缕82年的清风6 小时前
Windsurf 凭什么敢叫板 Cursor?深度实测一周,聊聊底层机制与真实心路
claude·cursor
乘风gg6 小时前
一个人一个月合并 2500 PRs 的秘密是什么?
前端·ai编程·claude
一缕82年的清风1 天前
上下文工程(Context Engineering):为什么说它是 AI 编程时代最重要的核心内功?
claude·cursor
云卷云舒___________2 天前
突发 | OpenAI 惊现 memory4-dream 与 gpt-rosalind!Gemini 3.8 路由至4? | 10月11日 AI日报
openai·codex·gemini·ai日报·antigravity·memory4dream·gptrosalind
向霸天2 天前
从Claude code来看业务中如何设计一个agent的核心部分
claude
冬奇Lab2 天前
一天一个开源项目(第234篇):CC Switch —— 一个 App 管理十款 AI 编程工具的 Provider、MCP、Skills 和 Prompts
人工智能·开源·claude
OliverQiu2 天前
开源一个 macOS 小工具:集中管理 ChatGPT 的网络启动配置
macos·chatgpt·swiftui·mac·proxy·codex·dmg