Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/vendor-superjson-cjs-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@trigger.dev/core": patch
---

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.
19 changes: 15 additions & 4 deletions packages/core/src/v3/imports/superjson-cjs.cts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
// @ts-ignore
const { default: superjson } = require("superjson");
/**
* Vendored superjson import (CJS version)
*
* This module provides a bundled version of superjson that works in CJS
* environments without relying on Node.js's experimental require(ESM) feature.
*
* The bundle is created from superjson@2.2.1 using esbuild.
*/

// @ts-ignore
superjson.registerCustom<Buffer, number[]>(
// @ts-ignore - Pre-built bundle doesn't have TS declarations
// eslint-disable-next-line @typescript-eslint/no-require-imports
const superjson = require("../vendor/superjson.cjs");

// Register Buffer serialization for Node.js environments
// @ts-ignore - Type assertions not needed for runtime behavior
superjson.registerCustom(
{
isApplicable: (v: unknown): v is Buffer => typeof Buffer === "function" && Buffer.isBuffer(v),
serialize: (v: Buffer) => [...v],
Expand Down
21 changes: 15 additions & 6 deletions packages/core/src/v3/imports/superjson.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
// @ts-ignore
import superjson from "superjson";
/**
* Vendored superjson import
*
* This module provides a bundled version of superjson that works in both ESM and CJS
* environments without relying on Node.js's experimental require(ESM) feature.
*
* The bundle is created from superjson@2.2.1 using esbuild.
*/

// @ts-ignore - Pre-built bundle doesn't have TS declarations
import superjson from "../vendor/superjson.js";

// Register Buffer serialization for Node.js environments
superjson.registerCustom<Buffer, number[]>(
{
isApplicable: (v): v is Buffer => typeof Buffer === "function" && Buffer.isBuffer(v),
serialize: (v) => [...v],
deserialize: (v) => Buffer.from(v),
isApplicable: (v: unknown): v is Buffer => typeof Buffer === "function" && Buffer.isBuffer(v),
serialize: (v: Buffer) => [...v],
deserialize: (v: number[]) => Buffer.from(v),
},
"buffer"
);

// @ts-ignore
export default superjson;
Loading
Loading