Skip to content

fix: ESM world loading for @workflow/world-postgres #836

Closed
michael-han-dev wants to merge 7 commits into
vercel:mainfrom
michael-han-dev:fix/esm-world-loading
Closed

fix: ESM world loading for @workflow/world-postgres #836
michael-han-dev wants to merge 7 commits into
vercel:mainfrom
michael-han-dev:fix/esm-world-loading

Conversation

@michael-han-dev
Copy link
Copy Markdown
Contributor

Description

@workflow/world-postgres fails with ERR_REQUIRE_ESM because world.ts used require() to load external worlds, but postgres is ESM-only. fixmade : replace require() with dynamic import() via new Function() to bypass bundler static analysis. Added initWorld() for async world loading. External worlds now need await initWorld() instead of getWorld() - though this was already broken before, so nothing that was working is changing. Built-in worlds (local, vercel) unchanged. updated docs accordingly as well. fixes #812

How did you test your changes?

Added packages/core/src/runtime/world.test.ts with 13 tests
Postgres World Testing
Ran a local postgres container (postgres:18-alpine) on port 5432.

Framework Tests
Built and started each framework with WORKFLOW_TARGET_WORLD=@workflow/world-postgres and the postgres connection string. All four logged "Starting Postgres World..." and started without ESM errors.

  • Nitro v3: production build, ran node .output/server/index.mjs, server on localhost:3000
  • Nuxt: production build, ran node .output/server/index.mjs, server on localhost:3000
  • SvelteKit: production build, ran node build/index.js, server on localhost:3000
  • Astro: production build, ran bundled output, server on localhost:4321

The Astro start-with-pg.mjs script fails with ERR_MODULE_NOT_FOUND. Pre-existing issue unrelated to this PR. Bundled output works.

Edge Cases
Tested invalid module path (throws ERR_MODULE_NOT_FOUND), module with wrong exports (throws "Invalid target world module"), external world accessed before init (throws "Call await initWorld() first"), and built-in worlds (still work synchronously).

E2E with Postgres
Pushed the database schema via pnpm db:push in world-postgres. Started Next.js Turbopack with postgres world env vars. Triggered the simple workflow via curl to /api/trigger. Got back a run ID.

Verified in postgres: workflow run created, step created, pg-boss jobs queued and processed. The ESM module loaded correctly and the World instance worked.

Also ran the same workflow with local world to confirm end-to-end execution works. It does.

PR Checklist - Required to merge

  • 📦 pnpm changeset was run to create a changelog for this PR
    • During beta, we only use "patch" mode for changes. Don't tag minor/major versions.
    • Use pnpm changeset --empty if you are changing documentation or workbench apps
  • 🔒 DCO sign-off passes (run git commit --signoff on your commits)

Signed-off-by: voyager14 <21mh124@queensu.ca>
Signed-off-by: voyager14 <21mh124@queensu.ca>
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Jan 23, 2026

🦋 Changeset detected

Latest commit: 9f49cee

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 13 packages
Name Type
workflow Patch
@workflow/core Patch
@workflow/cli Patch
@workflow/ai Patch
@workflow/docs-typecheck Patch
@workflow/world-testing Patch
@workflow/builders Patch
@workflow/next Patch
@workflow/nitro Patch
@workflow/web-shared Patch
@workflow/astro Patch
@workflow/sveltekit Patch
@workflow/nuxt Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented Jan 23, 2026

@michael-han-dev is attempting to deploy a commit to the Vercel Labs Team on Vercel.

A member of the Team first needs to authorize it.

Comment thread packages/core/src/runtime.ts
Resolved conflicts:
- packages/core/src/runtime/world.ts: Updated createVercelWorldFromEnv()
  to use main's new API (removed baseUrl and skipProxy)
- packages/cli/src/lib/inspect/setup.ts: Kept initWorld import for
  external ESM world support
@michael-han-dev michael-han-dev force-pushed the fix/esm-world-loading branch 3 times, most recently from 65002aa to 2a8033e Compare January 26, 2026 19:14
michael-han-dev and others added 3 commits January 26, 2026 14:32
Signed-off-by: voyager14 <21mh124@queensu.ca>
Use require.resolve() from process.cwd() to resolve bare module
specifiers before importing. This fixes ERR_MODULE_NOT_FOUND in CI
where the dynamic import was resolving from packages/core context
instead of the app's node_modules.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Signed-off-by: voyager14 <21mh124@queensu.ca>
Signed-off-by: voyager14 <21mh124@queensu.ca>
Signed-off-by: voyager14 <21mh124@queensu.ca>
@VaguelySerious
Copy link
Copy Markdown
Member

Hi @michael-han-dev, we're currently discussing changes to the getWorld interface and world import system with the team, and this PR (and the underlying issue) is part of what's being considered. I'll get back to you soon

@VaguelySerious
Copy link
Copy Markdown
Member

VaguelySerious commented Feb 5, 2026

#942 refactors getWorld to be async without adding a separate initWorld, largely based on your changes. Testing that right now.

@pranaygp
Copy link
Copy Markdown
Contributor

pranaygp commented Feb 5, 2026

Yeah #942 might be the right approach but also still needs discussion. we def. don't want to add yet another world instantiation functiongetWorld, initWorld, createWorld 😭

@pranaygp pranaygp closed this Feb 5, 2026
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.

@workflow/world-postgres: ERR_REQUIRE_ESM with Next.js 15

3 participants