Skip to content

bin/opencode fails with 'require is not defined' in ESM packages #28343

@qiushile

Description

@qiushile

Problem

Running opencode --version on Node.js environments fails with:

ReferenceError: require is not defined in ES module scope

Root Cause

packages/opencode/bin/opencode is a JavaScript launcher using CommonJS require() syntax, but package.json declares "type": "module". Node.js resolves this file as ESM and require() is unavailable.

This affects all platforms (Linux, macOS, Windows) and any Node.js environment.

Reproduction

  1. Install opencode package from npm or clone repo
  2. Run node packages/opencode/bin/opencode --version (or any npm/pnpm/bun install that resolves the bin entry)
  3. Error: require is not defined in ES module scope

Expected Behavior

The launcher should execute successfully and spawn the platform-specific binary.

Fix

Rename bin/opencodebin/opencode.cjs and update the bin field in package.json. The .cjs extension tells Node.js to treat the file as CommonJS regardless of package type. 1 file rename + 1 line change.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions