Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Worker is not defined for target Node.js #12299

Closed
kevzettler opened this issue Dec 31, 2020 · 5 comments
Closed

Worker is not defined for target Node.js #12299

kevzettler opened this issue Dec 31, 2020 · 5 comments

Comments

@kevzettler
Copy link

Bug report

I am trying to use the new Webpack 5 native worker features. per @alexander-akait 's comment at webpack-contrib/worker-loader#301 (comment)

I was under the impression the worker support would work for both web workers and node.js worker-threads.
I understand the documentation is not yet ready and hope i'm missing something obvious.

What is the current behavior?
ReferenceError: Worker is not defined

in a target:node build

If the current behavior is a bug, please provide the steps to reproduce.

I have created a minimal reproduction repository at: https://github.com/kevzettler/webpack5-worker-test

What is the expected behavior?
Per comment at: webpack-contrib/worker-loader#301 (comment)
and discussion at:
#6472 (comment)

I assumed new Worker(new URL("./worker", import.meta.url)) would work in a Node.js target

Other relevant information:
webpack version:

webpack 5.11.1
webpack-cli 4.3.1

Node.js version:
v12.18.1
Operating System:
macOs Mojave 10.14.6
Additional tools:

@kevzettler kevzettler changed the title Worker is not defined for target Node Worker is not defined for target Node.js Dec 31, 2020
@alexander-akait
Copy link
Member

Because Worker doesn't exists in Node.js you need to use, supported syntaxes

(but you can add own too), please look at Worker from worker_threads

@kevzettler
Copy link
Author

@alexander-akait I need help. How do I use this syntax?

 worker = new Worker from worker_threads(new URL('./server.worker', import.meta.url));

Is not valid and breaks typescript

(but you can add own too),

How would I do this?

@sokra
Copy link
Member

sokra commented Jan 3, 2021

Please also read the node.js documentation about how to use workers in node. It's a bit different compared to the browser.

import { Worker } from "worker_threads";

new Worker(new URL(...))

@alexander-akait
Copy link
Member

@kevzettler answer above

@kevzettler
Copy link
Author

thanks yall. got it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants