Skip to content

opencode launcher crashes on first run: ReferenceError: require is not defined in ESM module scope #280

Description

@aarontrowbridge

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

  • node packages/opencode/bin/opencode --version runs without a ReferenceError
  • node --check packages/opencode/bin/opencode passes (valid ESM)
  • No other lines of the bin script are modified

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghitlNeeds a human decision/review

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions