Windows 上 agent-browser 使用问题排查与解决
背景
agent-browser 是 Vercel 推出的一个 AI 浏览器自动化工具,它允许通过自然语言控制浏览器进行自动化操作。在 macOS 和 Linux 上安装使用都很顺利,但在 Windows 上遇到了一些坑。本文记录了完整的排查与解决过程。
环境信息
- 操作系统: Windows 11
- Node.js: 已安装
- 安装方式:
npm install -g agent-browser - Git for Windows: 已安装(Git Bash 位于
C:\Program Files\Git\)
问题一:安装 Chromium 浏览器失败
现象
> agent-browser install
报错:
&: C:\Users\<YourUsername>\AppData\Roaming\npm\agent-browser.ps1:24
Line |
24 | & "/bin/sh$exe" "$basedir/node_modules/agent-browser/bin/agent-b …
| ~~~~~~~~~~~~~
| The term '/bin/sh.exe' is not recognized as a name of a cmdlet, function, script file, or executable
排查过程
- 首先查看 npm 生成的 PowerShell 脚本:
# 文件位置: C:\Users\<YourUsername>\AppData\Roaming\npm\agent-browser.ps1
- 脚本内容(npm 自动生成):
#!/usr/bin/env pwsh
$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent
$exe=""
if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
$exe=".exe"
}
$ret=0
if (Test-Path "$basedir//bin/sh$exe") {
if ($MyInvocation.ExpectingInput) {
$input | & "$basedir//bin/sh$exe" "$basedir/node_modules/agent-browser/bin/agent-browser" $args
} else {
& "$basedir//bin/sh$exe" "$basedir/node_modules/agent-browser/bin/agent-browser" $args
}
$ret=$LASTEXITCODE
} else {
# 问题出在这里!
if ($MyInvocation.ExpectingInput) {
$input | & "/bin/sh$exe" "$basedir/node_modules/agent-browser/bin/agent-browser" $args
} else {
& "/bin/sh$exe" "$basedir/node_modules/agent-browser/bin/agent-browser" $args
}
$ret=$LASTEXITCODE
}
exit $ret
- 问题分析:脚本硬编码了 Unix 风格的
/bin/sh路径,Windows 上不存在该路径。 - 确认 Git Bash 的
sh.exe位置:
> where sh.exe
C:\Program Files\Git\usr\bin\sh.exe
解决方案
将脚本中的 /bin/sh$exe 替换为 Git Bash 的完整路径。
修改前(第 22-24 行):
if ($MyInvocation.ExpectingInput) {
$input | & "/bin/sh$exe" "$basedir/node_modules/agent-browser/bin/agent-browser" $args
} else {
& "/bin/sh$exe" "$basedir/node_modules/agent-browser/bin/agent-browser" $args
}
修改后:
if ($MyInvocation.ExpectingInput) {
$input | & "C:\Program Files\Git\usr\bin\sh.exe" "$basedir/node_modules/agent-browser/bin/agent-browser" $args
} else {
& "C:\Program Files\Git\usr\bin\sh.exe" "$basedir/node_modules/agent-browser/bin/agent-browser" $args
}
验证
> agent-browser install
Installing Chromium browser...
✓ Chromium installed successfully
问题二:运行命令时报错 Unix 命令未找到
现象
> agent-browser open www.baidu.com
报错:
C:\Users\<YourUsername>\AppData\Roaming\npm/node_modules/agent-browser/bin/agent-browser: line 11: dirname: command not found
C:\Users\<YourUsername>\AppData\Roaming\npm/node_modules/agent-browser/bin/agent-browser: line 13: uname: command not found
C:\Users\<YourUsername>\AppData\Roaming\npm/node_modules/agent-browser/bin/agent-browser: line 13: tr: command not found
Error: No binary found for -
Run 'npm run build:native' to build for your platform
排查过程
- 查看 shell 包装脚本的内容:
# 文件位置: C:\Users\<YourUsername>\AppData\Roaming\npm\node_modules\agent-browser\bin\agent-browser
#!/bin/sh
# agent-browser CLI wrapper
# Detects OS/arch and runs the appropriate native binary
SCRIPT="$0"
while [ -L "$SCRIPT" ]; do
SCRIPT_DIR="$(cd "$(dirname "$SCRIPT")" && pwd)"
SCRIPT="$(readlink "$SCRIPT")"
case "$SCRIPT" in /*) ;; *) SCRIPT="$SCRIPT_DIR/$SCRIPT" ;; esac
done
SCRIPT_DIR="$(cd "$(dirname "$SCRIPT")" && pwd)"
# 需要这些 Unix 命令
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m)
case "$OS" in darwin) OS="darwin" ;; linux) OS="linux" ;; mingw*|msys*|cygwin*) OS="win32" ;; esac
case "$ARCH" in x86_64|amd64) ARCH="x64" ;; aarch64|arm64) ARCH="arm64" ;; esac
BINARY="$SCRIPT_DIR/agent-browser-${OS}-${ARCH}"
if [ -f "$BINARY" ] && [ -x "$BINARY" ]; then
exec "$BINARY" "$@"
fi
echo "Error: No binary found for ${OS}-${ARCH}" >&2
echo "Run 'npm run build:native' to build for your platform" >&2
exit 1
- 问题分析:脚本需要
dirname、uname、tr等 Unix 命令来检测操作系统和架构。虽然 Git Bash 提供了这些命令,但脚本在检测 Windows 环境时可能失败。 - 检查实际存在的二进制文件:
> ls C:\Users\<YourUsername>\AppData\Roaming\npm\node_modules\agent-browser\bin\
agent-browser-darwin-arm64
agent-browser-darwin-x64
agent-browser-linux-arm64
agent-browser-linux-x64
agent-browser-win32-x64.exe # ← Windows 的二进制文件是存在的!
解决方案
既然 Windows 的二进制文件已经存在,我们可以让 PowerShell 脚本直接调用 Windows 二进制文件,完全绕过 shell 脚本的 OS 检测逻辑。
完整的修改后脚本
用以下内容完整替换 C:\Users\<YourUsername>\AppData\Roaming\npm\agent-browser.ps1:
#!/usr/bin/env pwsh
$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent
# Windows binary path
$binary="$basedir/node_modules/agent-browser/bin/agent-browser-win32-x64.exe"
$ret=0
if (Test-Path $binary) {
# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & $binary @args
} else {
& $binary @args
}
$ret=$LASTEXITCODE
} else {
Write-Error "Error: agent-browser Windows binary not found at $binary"
$ret=1
}
exit $ret
脚本修改说明
| 原脚本逻辑 | 新脚本逻辑 |
|---|---|
| 通过 sh.exe 调用 shell 脚本 | 直接调用 .exe 二进制文件 |
| shell 脚本检测 OS/架构 | 硬编码 Windows 路径 |
| 可能因命令缺失失败 | 无需外部命令,更可靠 |
验证
> agent-browser open www.baidu.com
✓ 百度一下,你就知道
https://www.baidu.com/
成功!
完整对比
原始脚本(npm 生成)
#!/usr/bin/env pwsh
$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent
$exe=""
if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
$exe=".exe"
}
$ret=0
if (Test-Path "$basedir//bin/sh$exe") {
if ($MyInvocation.ExpectingInput) {
$input | & "$basedir//bin/sh$exe" "$basedir/node_modules/agent-browser/bin/agent-browser" $args
} else {
& "$basedir//bin/sh$exe" "$basedir/node_modules/agent-browser/bin/agent-browser" $args
}
$ret=$LASTEXITCODE
} else {
if ($MyInvocation.ExpectingInput) {
$input | & "/bin/sh$exe" "$basedir/node_modules/agent-browser/bin/agent-browser" $args
} else {
& "/bin/sh$exe" "$basedir/node_modules/agent-browser/bin/agent-browser" $args
}
$ret=$LASTEXITCODE
}
exit $ret
修改后脚本
#!/usr/bin/env pwsh
$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent
# Windows binary path
$binary="$basedir/node_modules/agent-browser/bin/agent-browser-win32-x64.exe"
$ret=0
if (Test-Path $binary) {
# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & $binary @args
} else {
& $binary @args
}
$ret=$LASTEXITCODE
} else {
Write-Error "Error: agent-browser Windows binary not found at $binary"
$ret=1
}
exit $ret
总结
在 Windows 上使用 agent-browser 遇到的两个问题,本质上都是因为 npm 生成的 PowerShell 包装脚本过于依赖 Unix 环境。
核心解决思路:既然 Windows 原生二进制文件已经存在,直接调用它即可,无需通过 shell 脚本做 OS 检测。
注意事项:
- 修改后的脚本在
agent-browser更新时会被覆盖,需要重新修改 - 如果你的 Git 安装路径不同,需要相应调整路径
- ARM64 Windows 用户需要将脚本中的
x64改为arm64
替代方案(不想修改脚本的话):
- 使用 Git Bash 终端运行命令
- 使用 npx:
npx agent-browser <command> - 直接调用二进制文件
相关链接
作者:kissazi2
出处:http://www.cnblogs.com/kissazi2/
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

浙公网安备 33010602011771号