Skip to content

Fix plugin loading error ('fn4 is not a function') via bundling#499

Merged
Tarquinen merged 4 commits intoOpencode-DCP:devfrom
NetroAki:fix/esm-module-resolution
Apr 12, 2026
Merged

Fix plugin loading error ('fn4 is not a function') via bundling#499
Tarquinen merged 4 commits intoOpencode-DCP:devfrom
NetroAki:fix/esm-module-resolution

Conversation

@NetroAki
Copy link
Copy Markdown
Contributor

Problem

When opencode (which runs on Bun) attempts to load the @tarquinen/opencode-dcp plugin, it crashes with the error:
fn4 is not a function. (In 'fn4(input)', 'fn4' is an instance of Object) failed to load plugin

This obscure internal Bun/JIT error occurs when it fails to resolve ESM imports during plugin instantiation. The compiled dist/ output from standard tsc contains extensionless relative imports (e.g. import { getConfig } from "./lib/config" instead of ./lib/config.js), which breaks modern Node.js and Bun strict ESM module resolution.

Additionally, the dependency jsonc-parser ships a broken ESM build that contains these exact same extensionless errors internally, meaning even patching the local TS imports wouldn't fully fix it.

Solution

Instead of emitting raw transpiled files via tsc (which would require rewriting all internal imports and abandoning jsonc-parser), this PR uses tsup to bundle the plugin into a single, clean dist/index.js file.

  • Added tsup for compilation and bundling.
  • Inlined jsonc-parser explicitly in tsup.config.ts so its internal imports are resolved ahead of time.
  • Preserved .d.ts declaration emission via tsc --emitDeclarationOnly.

This fixes the plugin initialization errors out-of-the-box for opencode users.

Verification

  • Run npm run build
  • Run node -e "import('./dist/index.js').then(m=>console.log('ok')).catch(console.error)"
  • Previously, this threw ERR_MODULE_NOT_FOUND due to the extensionless imports. Now it resolves properly without error.

This fixes the 'fn4 is not a function' error when the plugin is loaded by opencode (which runs on Bun).

The issue occurs because the generated JavaScript files from standard `tsc` contain extensionless relative imports (e.g. `import { ... } from "./lib/config"`), which breaks Node.js ESM module resolution requirements. It also breaks for the `jsonc-parser` dependency which ships an ESM build with the same issue.

By using `tsup` to bundle the project into a single `dist/index.js` file and inlining `jsonc-parser`, we eliminate all relative ESM import errors entirely while keeping the source code clean.
@Tarquinen Tarquinen changed the base branch from master to dev April 12, 2026 01:31
@Tarquinen
Copy link
Copy Markdown
Collaborator

Yea this actually looks good, thank you for your help. Not sure yet when I'll do another release as there's a number of other things that need to be fixed due to opencode plugin and sdk changes but should be soon.

@Tarquinen Tarquinen merged commit bf7f372 into Opencode-DCP:dev Apr 12, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants