fix: ESM world loading for @workflow/world-postgres #836
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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-postgresand the postgres connection string. All four logged "Starting Postgres World..." and started without ESM errors.node .output/server/index.mjs, server on localhost:3000node .output/server/index.mjs, server on localhost:3000node build/index.js, server on localhost:3000The Astro
start-with-pg.mjsscript fails withERR_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:pushin 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 changesetwas run to create a changelog for this PRpnpm changeset --emptyif you are changing documentation or workbench appsgit commit --signoffon your commits)