Repository navigation
Description
Summary
The manual recovery step recommended in #55888 - running node postinstall.mjs inside the installed @opencode/cli package - cannot work on Windows when invoked outside an npm lifecycle context.
postinstall.mjs only resolves the npm CLI correctly when npm_execpath is set. Otherwise it falls back to spawnSync("npm", ...), which always fails with ENOENT on Windows because the npm shim is npm.cmd.
The failure is silent: installPackage() treats a non-zero status as a failed install and continues, so the user only ever sees the generic Failed to install OpenCode. Try manually installing ... message, with no npm output and no hint of the real cause.
Environment
- opencode version: 2.0.31
- OS: Windows 11 (win32 x64)
- Node: v24.18.0
- npm: 11.16.0
- Install method: global npm install (
npm i -g @opencode/cli) npm_execpath: unset (becausepostinstall.mjswas run directly withnode)
Root cause
packages/cli/postinstall.mjs:
// Windows cannot spawn npm's .cmd shim directly, so reuse the npm CLI that runs this script.
const npm = process.env.npm_execpath?.endsWith("npm-cli.js")
? [process.execPath, process.env.npm_execpath]
: ["npm"]The comment states the intent, but the ["npm"] fallback is unreachable on Windows. childProcess.spawnSync("npm", ...) without shell: true does not resolve .cmd shims through PATHEXT.
Reproduction
PS> cd $env:APPDATA\npm\node_modules\@opencode\cli
PS> node postinstall.mjs
Failed to install OpenCode. Try manually installing "@opencode/cli-windows-x64" or "@opencode/cli-windows-x64-baseline".
# exit code 1, ~0.4s, no npm outputEvidence
Same machine, same session:
npm_execpath = undefined
spawnSync("npm", ...) -> status = null error = ENOENT
spawnSync("npm.cmd", ...) -> status = null error = EINVAL
spawnSync(process.execPath, [npmCliJs, "--version"]) -> status = 0 stdout = "11.16.0"
Setting npm_execpath makes the identical invocation succeed:
PS> $env:npm_execpath = "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js"
PS> node postinstall.mjs
# exit code 0Suggested fix
Resolve the npm CLI robustly instead of assuming it is directly spawnable:
- resolve
npm-cli.jsrelative toprocess.execPath(../node_modules/npm/bin/npm-cli.js), or - fall back to
npm.cmdwithshell: true, or - resolve through
PATHEXT
Separately, consider surfacing result.error when installPackage fails - the current message hides ENOENT entirely, which makes this hard to diagnose.
Additional context
This machine was in the state described by #55888 (bin/opencode.exe left as the 229-byte placeholder). Because node postinstall.mjs was the suggested recovery and it fails on Windows, the install could not be repaired without manually setting npm_execpath.
Related
- upgrade: npm upgrade skips postinstall and leaves an invalid opencode.exe on Windows #55888 (closed) - recommends
node postinstall.mjsas manual recovery - npm 12 upgrade leaves the Windows executable stub #51553 (open) - npm 12 upgrade leaves the Windows executable stub
Activity
opencode-agent commentedon Oct 17, 2026
Thanks for the detailed report and the evidence. I couldn't reproduce this here because it only happens on Windows and this sandbox runs Linux. On Linux,
npmis a normal executable, sospawnSync("npm", …)works. On Windows, npm is a.cmdshim that Node can't start without a shell.I did check the code. The behaviour you describe is still there on the latest
dev(a49f179), andpackages/cli/script/postinstall.mjsis unchanged since v2.0.31:npm_execpathisn't set, the script falls back to["npm"](line 25).installPackage()returnsfalseon a non-zero status without printingresult.error, so theENOENTis hidden behind the genericFailed to install OpenCode…message.A maintainer with a Windows machine will need to confirm this. Until then, setting
npm_execpathto npm'snpm-cli.jsbefore runningnode postinstall.mjsworked for you, so that is the workaround.Related: #51553 is the upstream cause, where npm 12 upgrades leave the stub
opencode.exe. #55888 is wherenode postinstall.mjswas suggested as the fix.deathman915-sys commentedon Nov 2, 2026
Refinement from a Windows reproduction on the current release: the manual recovery does work, provided the platform package is already present.
Environment (per #51553:
bin\opencode.exeleft as the 229-byte stub)npm_execpathunset (invoked directly asnode ./postinstall.mjs)Result:
bin\opencode.exe= 259,169,160 bytes, hard-linked tocli-windows-x64\bin\opencode.exe;opencode --version→opencode v2.2.5.Why it worked here, and where the ENOENT path is actually reached:
main()triesresolveBinary(name)+copyBinary(name)first, and only falls back toinstallPackage(name)when that throws.installPackage()is the sole caller ofspawnSync(npm, ...), so it is the only placenpm_execpathmatters.In the
opencode upgradefailure mode the optional platform packages are installed correctly at the new version, soresolveBinary()resolves andcopyBinary()hard-links the real binary — npm is never spawned, and the documented recovery succeeds. Thenpmfallback is only reached when the platform package is missing or its version does not matchoptionalDependencies(whichresolveBinary()also rejects).So the two reports differ in precondition rather than contradicting each other:
installPackage()runs, hits the unreachable-on-Windowsnpmfallback, and fails silently with the generic message.The suggested fix to resolve
npm-cli.jsrelative toprocess.execPath(or useshell: true/ resolve viaPATHEXT) still stands for the second case, and surfacingresult.errorwould make it diagnosable.