Skip to content

cli: manual recovery node postinstall.mjs fails on Windows - npm_execpath unset causes silent spawnSync("npm") ENOENT #56380

Open

Description

@huta-creative-studio

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 (because postinstall.mjs was run directly with node)

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 output

Evidence

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 0

Suggested fix

Resolve the npm CLI robustly instead of assuming it is directly spawnable:

  • resolve npm-cli.js relative to process.execPath (../node_modules/npm/bin/npm-cli.js), or
  • fall back to npm.cmd with shell: 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

Activity

  1. opencode-agent commented on Oct 17, 2026

    @opencode-agent
    Contributor

    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, npm is a normal executable, so spawnSync("npm", …) works. On Windows, npm is a .cmd shim 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), and packages/cli/script/postinstall.mjs is unchanged since v2.0.31:

    • If npm_execpath isn't set, the script falls back to ["npm"] (line 25).
    • installPackage() returns false on a non-zero status without printing result.error, so the ENOENT is hidden behind the generic Failed to install OpenCode… message.

    A maintainer with a Windows machine will need to confirm this. Until then, setting npm_execpath to npm's npm-cli.js before running node postinstall.mjs worked for you, so that is the workaround.

    Related: #51553 is the upstream cause, where npm 12 upgrades leave the stub opencode.exe. #55888 is where node postinstall.mjs was suggested as the fix.

  2. deathman915-sys commented on Nov 2, 2026

    @deathman915-sys

    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.exe left as the 229-byte stub)

    • opencode: 2.2.2 → 2.2.5, Windows 10.0.19045.7807 (win32 x64), npm 12.2.1
    • npm_execpath unset (invoked directly as node ./postinstall.mjs)
    cd "$env:APPDATA\npm\node_modules\@opencode\cli"
    node ./postinstall.mjs   # exit 0

    Result: bin\opencode.exe = 259,169,160 bytes, hard-linked to cli-windows-x64\bin\opencode.exe; opencode --version → opencode v2.2.5.

    Why it worked here, and where the ENOENT path is actually reached: main() tries resolveBinary(name) + copyBinary(name) first, and only falls back to installPackage(name) when that throws. installPackage() is the sole caller of spawnSync(npm, ...), so it is the only place npm_execpath matters.

    In the opencode upgrade failure mode the optional platform packages are installed correctly at the new version, so resolveBinary() resolves and copyBinary() hard-links the real binary — npm is never spawned, and the documented recovery succeeds. The npm fallback is only reached when the platform package is missing or its version does not match optionalDependencies (which resolveBinary() also rejects).

    So the two reports differ in precondition rather than contradicting each other:

    • platform package present (the common post-upgrade case) → recovery works, no npm needed;
    • platform package absent or version-mismatched → installPackage() runs, hits the unreachable-on-Windows npm fallback, and fails silently with the generic message.

    The suggested fix to resolve npm-cli.js relative to process.execPath (or use shell: true / resolve via PATHEXT) still stands for the second case, and surfacing result.error would make it diagnosable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions