Skip to content

[Bug]: execute (Code Mode) runtime has no CommonJS require() — require('fs') throws ReferenceError #56811

Closed

Description

@dimahc

Summary

The execute tool's confined Code Mode runtime does not expose CommonJS require(). Any snippet that calls require('fs'), require('child_process'), etc. fails with:

ReferenceError: Unknown identifier 'require'. (line 1, col 12)

Reproduction

Call execute with:

const fs = require('fs');
console.log(fs.readdirSync('.'));

Expected: directory listing.
Actual: ReferenceError: Unknown identifier 'require'. (line 1, col 12)

Tool-description mismatch

The tool description states: "Imports, direct filesystem access, and timers are unavailable; all other external access goes through tools." That is only partially accurate — fetch is available, but require() is not available at all, so even pure logic that happens to load a module cannot run. Users are left guessing which globals exist.

Impact

Many realistic scripting workflows in Code Mode are blocked outright (reading files, spawning processes, path math). The gap is undocumented and not discoverable from the tool description.

Suggested fix

Either:

  • expose a minimal require() shim for node built-in modules (fs, path, child_process, os), or
  • document the limitation clearly in the tool description and ship a stable tools.* filesystem/process helper so users are not left guessing.

Environment

  • Host: OpenCode (anomalyco/opencode), plugin oh-my-opencode-slim loaded.

Activity

  1. changed the title [-][Bug]: execute (Code Mode) runtime has no CommonJS — throws ReferenceError[/-] [+][Bug]: execute (Code Mode) runtime has no CommonJS require() — require('fs') throws ReferenceError[/+] on Oct 18, 2026
  2. opencode-agent commented on Oct 18, 2026

    @opencode-agent
    Contributor

    Thanks for the detailed write-up, @dimahc!

    This is intended behaviour, not a bug. Code Mode's execute runs in OpenCode's own sandboxed JavaScript interpreter, not in Node. It deliberately has no module system (require/import), so access to files and processes goes through the tools in the Code Mode catalog, where OpenCode's permissions still apply. The tool description already says "Imports, direct filesystem access, and timers are unavailable", which is why require shows up as an unknown identifier.

    I'm treating this as a feature request with two possible directions:

    • a require shim for Node built-ins (fs, path, child_process, os) inside Code Mode, or
    • a clearer description of which globals are available, plus reliable filesystem/process helpers under tools.*.

    Maintainers will consider it. In the meantime, the regular file and shell tools (outside execute, or through tools.* when they're listed in the Code Mode catalog) are the way to read files and run commands.

    Related issues and PRs:

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