developit / web-worker

Consistent Web Workers in browser and Node.

Home Page:https://npm.im/web-worker

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Importing ES6 modules does not work on Windows

mmomtchev opened this issue · comments

Importing a worker in ES6 mode fails on Windows:

worker = new Worker(new URL('./worker.js', import.meta.url), {type: 'module'});

ends with:

Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only URLs with a scheme in: file, data are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'c:'
    at new NodeError (node:internal/errors:400:5)
    at throwIfUnsupportedURLScheme (node:internal/modules/esm/resolve:1055:11)
    at defaultResolve (node:internal/modules/esm/resolve:1135:3)
    at nextResolve (node:internal/modules/esm/loader:163:28)
    at defer (C:\Users\mmom\src\sqlite-wasm-http\node_modules\ts-node\src\esm.ts:159:23)
    at entrypointFallback (C:\Users\mmom\src\sqlite-wasm-http\node_modules\ts-node\src\esm.ts:168:34)
    at C:\Users\mmom\src\sqlite-wasm-http\node_modules\ts-node\src\esm.ts:202:16
    at addShortCircuitFlag (C:\Users\mmom\src\sqlite-wasm-http\node_modules\ts-node\src\esm.ts:409:21)
    at resolve (C:\Users\mmom\src\sqlite-wasm-http\node_modules\ts-node\src\esm.ts:197:12)
    at nextResolve (node:internal/modules/esm/loader:163:28) {
  code: 'ERR_UNSUPPORTED_ESM_URL_SCHEME'
}

The problem is nodejs/node#31710
(yes, initially, mostly everyone is as surprised as I was - but it seems that everyone agrees that this is how it should be)

It comes from this import:

import(mod)

Which happens after removing the file:/// part here:

mod = URL.fileURLToPath(new URL.URL(url, baseUrl));

I have been stuck on this error for half a day and I am glad to see it here. I tried replacing line 178 of my local node.js file but it's still throwing the same error.

Is there a known work around for this until its fixed in the release version?

mention the fix in the pull request does not address the commonjs node file

Question why is my esm project even calling into the cjs file? I have my type set to module

In the meantime, I have @mmomtchev/web-worker

I will add your fix for the CJS version

However, it would be also great to hear from the module maintainers? Is this module still maintained? It seems to be somewhat popular.

I am surprised that something as fundamental as this has been left out to gather dust. Now that we have fetch in Node.js, this is the last significant blocker for low-level browser projects that do not manipulate the DOM.

Isn't it time to have a grand unified API? I would even dare to suggest that the ES specification should probably include multithreading with a standard import mechanism. What do the high priests of the TSC think about this?

Especially, since there is already a de-facto standard:

const worker = new Worker( new URL( './worker.js', import.meta.url ) )

Everything needed is to write it down as such and implement WebWorker in Node.js.