kind: personal
status: draft
repo: harmoniqs/opencode
labels: [bug]
created: 2026-09-01
bin/opencode crashes on first run: require is not defined in ESM scope
Problem
packages/opencode/bin/opencode uses bare require("child_process") on line 3,
but packages/opencode/package.json declares "type": "module" — so the launcher
is parsed as ESM and dies on first run with:
ReferenceError: require is not defined in ESM module scope
Every invocation of the opencode CLI is affected, including --version — the
crash happens at module top level, before any command handling runs.
Approach
Swap the bare require for a namespace import from node:child_process, keeping
the rest of line 3 (and lines 4–5) untouched:
-const childProcess = require("child_process")+import childProcess from "node:child_process"Minimal one-line patch; no behavior change beyond the launcher actually starting.
Acceptance Criteria
Testing Decisions
- Reuse-first: check for an existing ESM-interop helper (
createRequire idiom)
before hand-rolling — per the constraints skill, never invent what a helper carries.
Key Decisions
- Namespace default import (
import childProcess from "node:child_process") over
createRequire(import.meta.url) — the default import is the smaller diff and
matches how the file's other imports are written.
Constraints & Invariants
"type": "module" in packages/opencode/package.json must remain true (invariant).- Do not reformat or touch lines 4–5 of the bin script.
Prior Art
packages/opencode/src/index.ts — the launcher's import block, written in ESM style.
Source
- Root cause found in a live verification run (2026-09-01);
node --check + --version
confirmed the ESM parse failure before this brief was drafted.
kind: personal
status: draft
repo: harmoniqs/opencode
labels: [bug]
created: 2026-09-01
bin/opencode crashes on first run:
require is not definedin ESM scopeProblem
packages/opencode/bin/opencodeuses barerequire("child_process")on line 3,but
packages/opencode/package.jsondeclares"type": "module"— so the launcheris parsed as ESM and dies on first run with:
Every invocation of the
opencodeCLI is affected, including--version— thecrash happens at module top level, before any command handling runs.
Approach
Swap the bare
requirefor a namespace import fromnode:child_process, keepingthe rest of line 3 (and lines 4–5) untouched:
Minimal one-line patch; no behavior change beyond the launcher actually starting.
Acceptance Criteria
node packages/opencode/bin/opencode --versionruns without a ReferenceErrornode --check packages/opencode/bin/opencodepasses (valid ESM)Testing Decisions
createRequireidiom)before hand-rolling — per the constraints skill, never invent what a helper carries.
Key Decisions
import childProcess from "node:child_process") overcreateRequire(import.meta.url)— the default import is the smaller diff andmatches how the file's other imports are written.
Constraints & Invariants
"type": "module"inpackages/opencode/package.jsonmust remain true (invariant).Prior Art
packages/opencode/src/index.ts— the launcher's import block, written in ESM style.Source
node --check+--versionconfirmed the ESM parse failure before this brief was drafted.