I am trying to use the web-worker npm package. However, I get the following error:

(node:48786) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
import URL from 'url';

SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:77:18)
    at wrapSafe (node:internal/modules/cjs/loader:1288:20)
    at Module._compile (node:internal/modules/cjs/loader:1340:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at cjsLoader (node:internal/modules/esm/translators:356:17)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:305:7)
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)

Node.js v20.11.0

My entire index.js consists of:

import Worker from "web-worker";

new Worker("./hello.js");

and I already set “type”:”module” on my package.json

    "name": "cash-machines",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "type": "module",
    "scripts": {
        "start": "node index.js",
        "test": "echo "Error: no test specified" && exit 1"
    "author": "",
    "license": "ISC",
    "dependencies": {
        "web-worker": "^1.3.0"

I’ve spent to much time trying to figure how to solve this, why does this happen?

I see from the package.json of the web-worker package that it has a

"exports": {
    "types": "./index.d.ts",
    "import": {
      "bun": "./browser.js",
      "browser": "./browser.js",
      "default": "./node.js"
    "module": {
      "bun": "./browser.js",
      "browser": "./browser.js",
      "default": "./node.js"
    "default": {
      "bun": "./cjs/browser.js",
      "browser": "./cjs/browser.js",
      "default": "./cjs/node.js"

So, in theory, I should be able to import the package, and the error stems from ./node.js which is a supposedly a module, but I don’t know why it is not treated like one.

New contributor

Luigui1729 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.