Hi there! I have recently started working with cloudflare workers and currently am in the process of porting my Remix.run app to work with workers. However, I seem to have come to an impasse during debugging the miniflare build process.
I am using esbuild to build the worker, following is the build code:
// build.ts
import * as esbuild from "esbuild";
const mode = process.env.NODE_ENV?.toLowerCase() ?? "development";
console.log(`[Worker] Running esbuild in ${mode} mode`);
esbuild.build({
entryPoints: ["./worker/index.ts"],
bundle: true,
minify: mode === "production",
platform: "node",
format: "esm",
define: {
"process.env.NODE_ENV": `"${mode}"`,
},
outfile: "./worker/dist/worker.js",
});
there seems to be no issue with the build process, but when miniflare tries to parse the worker.js file, it throws the following error:
[miniflare] [mf:inf] Listening on :8787
[miniflare] [mf:inf] - http://127.0.0.1:8787
[miniflare] [mf:inf] - http://10.1.1.250:8787
[miniflare] [mf:err] Unable to parse worker/dist/worker.js: SyntaxError: Cannot use import statement outside a module (ignoring)
and here is a snippet of my remix app's package json file:
{
"private": true,
"sideEffects": false,
"main": "./worker/dist/worker.js",
"scripts": {
...
"build:css": "postcss styles/app.css -o app/styles/app.css",
"build:remix": "remix build",
"build:worker": "NODE_ENV=production ts-node ./build.ts",
"build": "npm run build:css && npm run build:remix && npm run build:worker",
"dev:css": "postcss styles/app.css -o app/styles/app.css -w",
"dev:miniflare": "miniflare --build-command \"npx ts-node ./build.ts\" --watch",
"dev:remix": "remix watch",
"dev": "NODE_ENV=development concurrently \"npm:dev:*\"",
"postinstall": "remix setup cloudflare-workers",
"deploy": "wrangler publish",
"start": "miniflare"
},}
Any help on solving this issue would be greatly appreciated!