[PR #2419] feat: make npm package install work on windows #10308

Closed
opened 2026-02-16 18:14:56 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/2419

State: closed
Merged: Yes


Current Issue

Installing opencode via <npm|bun> install -g opencode-ai on Windows is a little broken (#631)

Output -

& : The term '/bin/sh.exe' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\Username\AppData\Roaming\npm\opencode.ps1:24 char:7
+     & "/bin/sh$exe"  "$basedir/node_modules/opencode-ai/bin/opencode" ...
+       ~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (/bin/sh.exe:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

This happens because npm looks at the bin/opencode command which is a unix shell script, sees the #!/bin/sh shebang and generates wrappers for that instead of looking at bin/opencode.cmd.

Fix

  1. Add a preinstall.mjs hook to the package which only runs on Windows - this hook runs before the npm wrappers are generated and it's sole purpose is to the remove the bin/opencode unix shell script.
  2. Update postinstall.mjs to skip running on Windows. By avoiding binary symlinking, we ensure that the package's entrypoint is bin/opencode.cmd and that the namespace doesn't get muddled.
  3. Fixed batch file execution: Updated opencode.cmd to use start /b /wait to ensure the binary runs in the current terminal context across all shells (PowerShell, cmd, Git Bash)

NPM Wrapper

Before

...
if ($MyInvocation.ExpectingInput) {
  $input | & "/bin/sh$exe"  "$basedir/node_modules/opencode-ai/bin/opencode" $args
} else {
  & "/bin/sh$exe"  "$basedir/node_modules/opencode-ai/bin/opencode" $args
}

After

...
if ($MyInvocation.ExpectingInput) {
  $input | & "$basedir/node_modules/opencode-ai/bin/opencode.cmd"   $args
} else {
  & "$basedir/node_modules/opencode-ai/bin/opencode.cmd"   $args
}

I've tested this on Windows and MacOS with both npm and bun and it seems like the fix works on Windows and does not break anything on MacOS

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/2419 **State:** closed **Merged:** Yes --- ### Current Issue Installing opencode via `<npm|bun> install -g opencode-ai` on Windows is a little broken (#631) Output - ``` & : The term '/bin/sh.exe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At C:\Users\Username\AppData\Roaming\npm\opencode.ps1:24 char:7 + & "/bin/sh$exe" "$basedir/node_modules/opencode-ai/bin/opencode" ... + ~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (/bin/sh.exe:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException ``` This happens because npm looks at the `bin/opencode` command which is a unix shell script, sees the `#!/bin/sh` shebang and generates wrappers for that instead of looking at `bin/opencode.cmd`. ### Fix 1. Add a `preinstall.mjs` hook to the package which only runs on Windows - this hook runs before the npm wrappers are generated and it's sole purpose is to the remove the `bin/opencode` unix shell script. 2. Update `postinstall.mjs` to skip running on Windows. By avoiding binary symlinking, we ensure that the package's entrypoint is `bin/opencode.cmd` and that the namespace doesn't get muddled. 3. Fixed batch file execution: Updated `opencode.cmd` to use `start /b /wait` to ensure the binary runs in the current terminal context across all shells (PowerShell, cmd, Git Bash) ### NPM Wrapper **Before** ```powershell ... if ($MyInvocation.ExpectingInput) { $input | & "/bin/sh$exe" "$basedir/node_modules/opencode-ai/bin/opencode" $args } else { & "/bin/sh$exe" "$basedir/node_modules/opencode-ai/bin/opencode" $args } ``` **After** ```powershell ... if ($MyInvocation.ExpectingInput) { $input | & "$basedir/node_modules/opencode-ai/bin/opencode.cmd" $args } else { & "$basedir/node_modules/opencode-ai/bin/opencode.cmd" $args } ``` I've tested this on Windows and MacOS with both npm and bun and it seems like the fix works on Windows and does not break anything on MacOS
yindo added the pull-request label 2026-02-16 18:14:56 -05:00
yindo closed this issue 2026-02-16 18:14:56 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#10308