Skip to content
Jabran Rafique

How to resolve Cloudflare Workers Sites deploy error in a Remix app

3 minutes read

If you run a Cloudflare Workers Site with Remix, there's a good chance you've hit, or will hit, this error when you deploy:

The uploaded script has no registered event handlers. [code: 10068]

It comes from wrangler v2. If any dependency in the Remix app uses ESM exports, then once everything is bundled into the worker script, wrangler guesses the worker format wrong.

This is how wrangler describes the guess, in node_modules/wrangler/src/entry.ts:

/**
 * A function to "guess" the type of worker.
 * We do this by running a lightweight build of the actual script,
 * and looking at the metafile generated by esbuild. If it has a default
 * export (or really, any exports), that means it's a "modules" worker.
 * Else, it's a "service-worker" worker. This seems hacky, but works remarkably
 * well in practice.
 */
export default async function guessWorkerFormat(
...

There's an open issue and a pull request for it in the wrangler repository.

Until that lands, you can patch wrangler yourself. There are two parts.

Part 1

You only need to do Part 1 once.

  • Install patch-package, which lets you patch broken node modules and keep the patch in your repo.
npm install patch-package
  • Open node_modules/wrangler/src/entry.ts and replace every:
format: 'esm';

with

format: hint === 'service-worker' ? 'cjs' : 'esm';
  • Do the same in node_modules/wrangler/wrangler-dist/cli.js. Replace every:
format: 'esm';

with

format: hint === 'service-worker' ? 'cjs' : 'esm';
  • Save the files
  • In a terminal, go to the root of the app
  • Run this and wait for it to create the patch
node_modules/.bin/patch-package wrangler

That creates a patch file at patches/wrangler+{version}.patch. It looks something like this:

Don't copy this. It's incomplete and only here for reference.

diff --git a/node_modules/wrangler/src/entry.ts b/node_modules/wrangler/src/entry.ts
index 3ee1afe..585cb84 100644
--- a/node_modules/wrangler/src/entry.ts
+++ b/node_modules/wrangler/src/entry.ts
@@ -151,7 +151,7 @@ export default async function guessWorkerFormat(
 		absWorkingDir: entryWorkingDirectory,
 		metafile: true,
 		bundle: false,
-		format: "esm",
+		format: hint === "service-worker" ? "cjs" : "esm",
 		target: "es2020",
 		write: false,
 		loader: {
diff --git a/node_modules/wrangler/wrangler-dist/cli.js b/node_modules/wrangler/wrangler-dist/cli.js
index 622e617..0090fbd 100644
--- a/node_modules/wrangler/wrangler-dist/cli.js
+++ b/node_modules/wrangler/wrangler-dist/cli.js
@@ -121579,7 +121579,7 @@ async function guessWorkerFormat(entryFile, entryWorkingDirectory, hint, tsconfi
     absWorkingDir: entryWorkingDirectory,
     metafile: true,
     bundle: false,
-    format: "esm",
+    format: hint === "service-worker" ? "cjs" : "esm",
     target: "es2020",
     write: false,
     loader: {
...

Part 2

  • Add this script to package.json:
"scripts": {
  "postinstall": "patch-package"
  ...
}

Now wrangler gets patched again after every npm install.

References