Ewok
Ewok
DDeno
Created by Ewok on 12/1/2024 in #help
worker with cjs code loaded from esm not working
const Worker = require("worker_threads");

const w = (new Worker(`const self = require("worker_threads");`, { eval: true }))();
const Worker = require("worker_threads");

const w = (new Worker(`const self = require("worker_threads");`, { eval: true }))();
error: Uncaught (in worker "[worker eval]") (in promise) ReferenceError: require is not defined
const self = require("worker_threads");
^
at data:text/javascript,const self = require("worker_threads");:1:14

info: Deno supports CommonJS modules in .cjs files, or when the closest
package.json has a "type": "commonjs" option.
hint: Rewrite this module to ESM,
or change the file extension to .cjs,
or add package.json next to the file with "type": "commonjs" option,
or pass --unstable-detect-cjs flag to detect CommonJS when loading.
docs: https://docs.deno.com/go/commonjs
error: Uncaught (in worker "[worker eval]") (in promise) ReferenceError: require is not defined
const self = require("worker_threads");
^
at data:text/javascript,const self = require("worker_threads");:1:14

info: Deno supports CommonJS modules in .cjs files, or when the closest
package.json has a "type": "commonjs" option.
hint: Rewrite this module to ESM,
or change the file extension to .cjs,
or add package.json next to the file with "type": "commonjs" option,
or pass --unstable-detect-cjs flag to detect CommonJS when loading.
docs: https://docs.deno.com/go/commonjs
--unstable-detect-cjs doesn't help. Deno 2.1.2 In my original project, the worker is in .cjs and imported from ESM but that doesn't seem to matter. Only import("node:worker_threads") works. But it is meant to support CJS. Also doesn't work without node: which is not normal too but I assume both are related to one thing.
3 replies