developit / web-worker

Consistent Web Workers in browser and Node.
https://npm.im/web-worker
Apache License 2.0
1.05k stars 55 forks source link

Importing ES6 modules does not work on Windows #35

Open mmomtchev opened 1 year ago

mmomtchev commented 1 year ago

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 https://github.com/nodejs/node/issues/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: https://github.com/developit/web-worker/blob/29fef9775702c91887d3d8733e595edf1a188f31/node.js#L178

Which happens after removing the file:/// part here: https://github.com/developit/web-worker/blob/29fef9775702c91887d3d8733e595edf1a188f31/node.js#L101

Firegarden commented 1 year ago

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?

Firegarden commented 1 year ago

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

mmomtchev commented 1 year ago

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.