325 字
1 分钟
opencode运行报错
2026-06-19

问题#

  • 系统: Ubuntu 24.04.4 LTS
  • 内核: Linux 6.8.0-124-generic
  • node.js版本: 24.14.0
  • pnpm版本: 11.8.0 在终端中执行opencode命令的时候,出现报错:
Error: opencode-ai's postinstall script was not run.
This occurs when using --ignore-scripts during installation, or when using a
package manager like pnpm that does not run postinstall scripts by default.
To fix this, run the postinstall script manually:
cd node_modules/opencode-ai && node postinstall.mjs
Or reinstall opencode-ai without the --ignore-scripts flag.

原因分析#

pnpm 出于安全考虑,默认跳过所有 postinstall脚本,这导致opencode-aipostinstall.mjs未执行,缺少初始化文件,导致CLI无法正常启动

排查流程#

  1. 确认opencode位置
which opencode
  1. 尝试找node_modules(失败)
pnpm root -g

输出/home/locxl/.local/share/pnpm/global/v11

ls /home/locxl/.local/share/pnpm/global/v11/

输出: 只有哈希命名的目录,没有node_mudules/opencode-ai

  1. 通过shim脚本找到真实路径
cat /home/locxl/.local/share/pnpm/opencode

输出:

#!/usr/bin/env bash
NODE_PATH='/home/locxl/.local/share/pnpm/global/5/.pnpm/opencode-ai@1.15.5/node_modules/opencode-ai/node_modules'
...

NODE_PATH提取真实包路径: /home/locxl/.local/share/pnpm/global/5/.pnpm/opencode-ai@1.15.5/node_modules/opencode-ai

  1. 手动执行postinstall脚本
cd /home/locxl/.local/share/pnpm/global/5/.pnpm/opencode-ai@1.15.5/node_modules/opencode-ai
node postinstall.mjs

执行成功

  1. 验证
opencode --version

输出: 1.15.5 成功

或者也可以#

启用pnpm脚本后重装

pnpm config set enable-pre-post-scripts true
pnpm add -g opencode-ai

之后所有pnpm全局安装的包都会自动执行postinstall,不会再遇到同类问题。

后记#

后来我发现这似乎是一个opencode 1.15.1版本后才引入的问题,因为v1.15.1起,opencode新增了postinstall.mjs负责下载平台特定Rust二进制,但是pnpm默认跳过了postinstall导致二进制未下载,使得只剩下一个空壳JS包装器,启动时候发现二进制不存在然后报错。 这在GitHub早已有了类似的issue: v1.15.1+ Breaks Bun Installs #27906:

opencode-ai’s postinstall script was not run - stuck after upgrade #29270

分享

如果这篇文章对你有帮助,欢迎分享给更多人!

opencode运行报错
https://blog.locxl.site/posts/opencode运行报错/
作者
locxl
发布于
2026-06-19
许可协议
CC BY-SA 4.0

部分信息可能已经过时

目录