Skip to content

Commit 3375d3d

Browse files
fix(core): vendor superjson to fix CJS compatibility issue
This fixes the ERR_REQUIRE_ESM error that occurs when using @trigger.dev/core in CJS environments with Node.js versions < 22.12. The superjson package (v2.x) is ESM-only, but Node.js doesn't support require() of ESM modules in older versions. The fix bundles superjson@2.2.1 using esbuild into both CJS and ESM formats, which are then imported by the existing wrapper modules. This ensures the package works correctly in all environments without relying on Node.js's experimental require(ESM) feature. Fixes #2937 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 4093883 commit 3375d3d

File tree

5 files changed

+1653
-10
lines changed

5 files changed

+1653
-10
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@trigger.dev/core": patch
3+
---
4+
5+
Vendor superjson to fix CJS compatibility issue
6+
7+
This fixes the `ERR_REQUIRE_ESM` error that occurs when using `@trigger.dev/core` in CJS environments with Node.js versions < 22.12. The superjson package (v2.x) is ESM-only, but Node.js doesn't support `require()` of ESM modules in older versions.
8+
9+
The fix bundles superjson@2.2.1 using esbuild into both CJS and ESM formats, which are then imported by the existing wrapper modules. This ensures the package works correctly in all environments without relying on Node.js's experimental `require(ESM)` feature.

packages/core/src/v3/imports/superjson-cjs.cts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1-
// @ts-ignore
2-
const { default: superjson } = require("superjson");
1+
/**
2+
* Vendored superjson import (CJS version)
3+
*
4+
* This module provides a bundled version of superjson that works in CJS
5+
* environments without relying on Node.js's experimental require(ESM) feature.
6+
*
7+
* The bundle is created from superjson@2.2.1 using esbuild.
8+
*/
39

4-
// @ts-ignore
5-
superjson.registerCustom<Buffer, number[]>(
10+
// @ts-ignore - Pre-built bundle doesn't have TS declarations
11+
// eslint-disable-next-line @typescript-eslint/no-require-imports
12+
const superjson = require("../vendor/superjson.cjs");
13+
14+
// Register Buffer serialization for Node.js environments
15+
// @ts-ignore - Type assertions not needed for runtime behavior
16+
superjson.registerCustom(
617
{
718
isApplicable: (v: unknown): v is Buffer => typeof Buffer === "function" && Buffer.isBuffer(v),
819
serialize: (v: Buffer) => [...v],
Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
1-
// @ts-ignore
2-
import superjson from "superjson";
1+
/**
2+
* Vendored superjson import
3+
*
4+
* This module provides a bundled version of superjson that works in both ESM and CJS
5+
* environments without relying on Node.js's experimental require(ESM) feature.
6+
*
7+
* The bundle is created from superjson@2.2.1 using esbuild.
8+
*/
39

10+
// @ts-ignore - Pre-built bundle doesn't have TS declarations
11+
import superjson from "../vendor/superjson.js";
12+
13+
// Register Buffer serialization for Node.js environments
414
superjson.registerCustom<Buffer, number[]>(
515
{
6-
isApplicable: (v): v is Buffer => typeof Buffer === "function" && Buffer.isBuffer(v),
7-
serialize: (v) => [...v],
8-
deserialize: (v) => Buffer.from(v),
16+
isApplicable: (v: unknown): v is Buffer => typeof Buffer === "function" && Buffer.isBuffer(v),
17+
serialize: (v: Buffer) => [...v],
18+
deserialize: (v: number[]) => Buffer.from(v),
919
},
1020
"buffer"
1121
);
1222

13-
// @ts-ignore
1423
export default superjson;

0 commit comments

Comments
 (0)