You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
lambda: handler.mjs crashes at import — CJS deps bundled into ESM without a __dirname shim (wawoff2) #1932
Every cloud render on a freshly deployed Lambda stack fails at handler import time:
ReferenceError: __dirname is not defined in ES module scope
at ../../node_modules/.bun/wawoff2@2.0.1/node_modules/wawoff2/build/compress_binding.js (file:///var/task/handler.mjs:71277:9)
at ../../node_modules/.bun/wawoff2@2.0.1/node_modules/wawoff2/compress.js
at ../producer/src/services/fontCompression.ts
Repro
CLI/packages 0.7.28, repo at 02e9d6142d8e3d812c1d8525753355793a3b9988 (also present on current main — packages/aws-lambda/scripts/build-zip.ts is unchanged since 9b34114d)
hyperframes lambda render <project> --width 1080 --height 1920 --fps 30 --wait → every Plan attempt fails with the ReferenceError above; execution ends FAILED
Root cause
build-zip.ts bundles the handler with esbuild format: "esm" and inlines everything except the chromium/puppeteer/aws-sdk externals. The font-resolution pipeline (#1255) pulled in wawoff2, whose emscripten CJS build references __dirname at module top level. Inlined into ESM output with no shim, the reference throws before the handler can do anything.
#1553 added the __filename/__dirname shim to the producer package's CJS banner — but the lambda handler bundle in build-zip.ts never got the equivalent, so deployed stacks are broken for any composition (the import chain handler → producer → fontCompression → wawoff2 runs unconditionally).
Fix that works (verified on a live stack)
An esbuild banner on the handler bundle is sufficient — wawoff2 embeds its wasm as a data URI, so no sidecar staging is needed:
// packages/aws-lambda/scripts/build-zip.ts — esbuild.build({ ... })format: "esm",banner: {js: "import{fileURLToPath as __hfFup}from'node:url';"+"import{dirname as __hfDn}from'node:path';"+"const __filename=__hfFup(import.meta.url);"+"const __dirname=__hfDn(__filename);",},
We're currently applying exactly this as a deploy-time patch in our CI and renders proceed past import. Happy to send a PR if useful.
Correction to the suggested fix: build-zip.ts already has a banner on the handler bundle (the createRequire shim for require) — so the fix is to extend that existing banner with the __filename/__dirname lines rather than add a new banner key (a duplicate key is silently overridden, which cost us a deploy cycle):
banner: {js: ["// hyperframes-aws-lambda handler bundle",'import { createRequire as __hf_createRequire } from "module";',"const require = __hf_createRequire(import.meta.url);",'import { fileURLToPath as __hf_fileURLToPath } from "url";','import { dirname as __hf_dirname } from "path";',"const __filename = __hf_fileURLToPath(import.meta.url);","const __dirname = __hf_dirname(__filename);",].join("\n"),},
Will confirm against a live stack shortly and report back.
Confirmed on a live stack: with the existing banner extended as above, the same render that failed at import now completes — 180/180 frames, 6s 1080x1920@30 composition, $0.0067. The banner extension is the complete fix; no sidecar staging needed (wawoff2 embeds its wasm as a data URI).
Activity
kmdesle commentedon Jul 4, 2026
Correction to the suggested fix:
build-zip.tsalready has abanneron the handler bundle (thecreateRequireshim forrequire) — so the fix is to extend that existing banner with the__filename/__dirnamelines rather than add a newbannerkey (a duplicate key is silently overridden, which cost us a deploy cycle):Will confirm against a live stack shortly and report back.
kmdesle commentedon Jul 4, 2026
Confirmed on a live stack: with the existing banner extended as above, the same render that failed at import now completes — 180/180 frames, 6s 1080x1920@30 composition, $0.0067. The banner extension is the complete fix; no sidecar staging needed (wawoff2 embeds its wasm as a data URI).
test(lambda): behavioral guard for handler CJS banner shims (#1977)
test(lambda): behavioral guard for handler CJS banner shims (heygen-c…
test(lambda): behavioral guard for handler CJS banner shims (heygen-c…