Next.js Discord

Discord Forum

Server-side worker threads: Cannot use import statement outside a module

Unanswered
Mugger Crocodile posted this in #help-forum
Open in Discord
Mugger CrocodileOP
I have a relatively standard Next.js application that uses typescript everywhere and I'm trying to offload some longish processes to a server-side worker thread.

The problem is that simply something like new Worker("../path/to/worker.ts") results in error complaining that it can't use imports outside of modules. Something like this:

Cannot use import statement outside a module
...lib/jobs/jobman-worker.ts:1
import { parentPort, Worker } from "worker_threads"
^^^^


I've tried switching to .mjs, .cjs or plain .js and either they give the same result or worse.

I have the feeling this might have to do with the bundling but I really have no idea.

Does anyone have any ideas or suggestions on what I'm doing wrong and what I can do to fix it?

0 Replies