1- # Worker
1+ # Worker Threads
22
33<!--introduced_in=REPLACEME-->
44
@@ -9,7 +9,7 @@ on independent threads, and to create message channels between them. It
99can be accessed using:
1010
1111```js
12- const worker = require('worker ');
12+ const worker = require('worker_threads ');
1313```
1414
1515Workers are useful for performing CPU-intensive JavaScript operations; do not
@@ -23,7 +23,9 @@ share memory efficiently by transferring `ArrayBuffer` instances or sharing
2323## Example
2424
2525```js
26- const { Worker, isMainThread, parentPort, workerData } = require('worker');
26+ const {
27+ Worker, isMainThread, parentPort, workerData
28+ } = require('worker_threads');
2729
2830if (isMainThread) {
2931 module.exports = async function parseJSAsync(script) {
@@ -104,7 +106,7 @@ yields an object with `port1` and `port2` properties, which refer to linked
104106[`MessagePort`][] instances.
105107
106108```js
107- const { MessageChannel } = require('worker ');
109+ const { MessageChannel } = require('worker_threads ');
108110
109111const { port1, port2 } = new MessageChannel();
110112port1.on('message', (message) => console.log('received', message));
@@ -241,8 +243,8 @@ Notable differences inside a Worker environment are:
241243
242244- The [`process.stdin`][], [`process.stdout`][] and [`process.stderr`][]
243245 may be redirected by the parent thread.
244- - The [`require('worker ').isMainThread`][] property is set to `false`.
245- - The [`require('worker ').parentPort`][] message port is available,
246+ - The [`require('worker_threads ').isMainThread`][] property is set to `false`.
247+ - The [`require('worker_threads ').parentPort`][] message port is available,
246248- [`process.exit()`][] does not stop the whole program, just the single thread,
247249 and [`process.abort()`][] is not available.
248250- [`process.chdir()`][] and `process` methods that set group or user ids
@@ -283,7 +285,9 @@ For example:
283285
284286```js
285287const assert = require('assert');
286- const { Worker, MessageChannel, MessagePort, isMainThread } = require('worker');
288+ const {
289+ Worker, MessageChannel, MessagePort, isMainThread
290+ } = require('worker_threads');
287291if (isMainThread) {
288292 const worker = new Worker(__filename);
289293 const subChannel = new MessageChannel();
@@ -292,7 +296,7 @@ if (isMainThread) {
292296 console.log('received:', value);
293297 });
294298} else {
295- require('worker ').once('workerMessage', (value) => {
299+ require('worker_threads ').once('workerMessage', (value) => {
296300 assert(value.hereIsYourPort instanceof MessagePort);
297301 value.hereIsYourPort.postMessage('the worker is sending this');
298302 value.hereIsYourPort.close();
@@ -309,9 +313,9 @@ if (isMainThread) {
309313 * `eval` {boolean} If true, interpret the first argument to the constructor
310314 as a script that is executed once the worker is online.
311315 * `data` {any} Any JavaScript value that will be cloned and made
312- available as [`require('worker ').workerData`][]. The cloning will occur as
313- described in the [HTML structured clone algorithm][], and an error will be
314- thrown if the object cannot be cloned (e.g. because it contains
316+ available as [`require('worker_threads ').workerData`][]. The cloning will
317+ occur as described in the [HTML structured clone algorithm][], and an error
318+ will be thrown if the object cannot be cloned (e.g. because it contains
315319 `function`s).
316320 * stdin {boolean} If this is set to `true`, then `worker.stdin` will
317321 provide a writable stream whose contents will appear as `process.stdin`
@@ -351,8 +355,8 @@ added: REPLACEME
351355* `value` {any} The transmitted value
352356
353357The `'message'` event is emitted when the worker thread has invoked
354- [`require('worker ').postMessage()`][]. See the [`port.on('message')`][] event
355- for more details.
358+ [`require('worker_threads ').postMessage()`][]. See the [`port.on('message')`][]
359+ event for more details.
356360
357361### Event: 'online'
358362<!-- YAML
@@ -371,8 +375,8 @@ added: REPLACEME
371375* `transferList` {Object[]}
372376
373377Send a message to the worker that will be received via
374- [`require('worker ').on('workerMessage')`][]. See [`port.postMessage()`][] for
375- more details.
378+ [`require('worker_threads ').on('workerMessage')`][].
379+ See [`port.postMessage()`][] for more details.
376380
377381### worker.ref()
378382<!-- YAML
@@ -444,7 +448,7 @@ added: REPLACEME
444448* {integer}
445449
446450An integer identifier for the referenced thread. Inside the worker thread,
447- it is available as [`require('worker ').threadId`][].
451+ it is available as [`require('worker_threads ').threadId`][].
448452
449453### worker.unref()
450454<!-- YAML
@@ -457,14 +461,14 @@ active handle in the event system. If the worker is already `unref()`ed calling
457461
458462[`Buffer`]: buffer.html
459463[`EventEmitter`]: events.html
460- [`MessagePort`]: #worker_class_messageport
461- [`port.postMessage()`]: #worker_port_postmessage_value_transferlist
462- [`Worker`]: #worker_class_worker
463- [`worker.terminate()`]: #worker_worker_terminate_callback
464- [`worker.postMessage()`]: #worker_worker_postmessage_value_transferlist_1
465- [`worker.on('message')`]: #worker_event_message_1
466- [`worker.threadId`]: #worker_worker_threadid_1
467- [`port.on('message')`]: #worker_event_message
464+ [`MessagePort`]: #worker_threads_class_messageport
465+ [`port.postMessage()`]: #worker_threads_port_postmessage_value_transferlist
466+ [`Worker`]: #worker_threads_class_worker
467+ [`worker.terminate()`]: #worker_threads_worker_terminate_callback
468+ [`worker.postMessage()`]: #worker_threads_worker_postmessage_value_transferlist_1
469+ [`worker.on('message')`]: #worker_threads_event_message_1
470+ [`worker.threadId`]: #worker_threads_worker_threadid_1
471+ [`port.on('message')`]: #worker_threads_event_message
468472[`process.exit()`]: process.html#process_process_exit_code
469473[`process.abort()`]: process.html#process_process_abort
470474[`process.chdir()`]: process.html#process_process_chdir_directory
@@ -473,11 +477,11 @@ active handle in the event system. If the worker is already `unref()`ed calling
473477[`process.stderr`]: process.html#process_process_stderr
474478[`process.stdout`]: process.html#process_process_stdout
475479[`process.title`]: process.html#process_process_title
476- [`require('worker ').workerData`]: #worker_worker_workerdata
477- [`require('worker ').on('workerMessage')`]: #worker_event_workermessage
478- [`require('worker ').postMessage()`]: #worker_worker_postmessage_value_transferlist
479- [`require('worker ').isMainThread`]: #worker_worker_ismainthread
480- [`require('worker ').threadId`]: #worker_worker_threadid
480+ [`require('worker_threads ').workerData`]: #worker_threads_worker_workerdata
481+ [`require('worker_threads ').on('workerMessage')`]: #worker_threads_event_workermessage
482+ [`require('worker_threads ').postMessage()`]: #worker_threads_worker_postmessage_value_transferlist
483+ [`require('worker_threads ').isMainThread`]: #worker_threads_worker_ismainthread
484+ [`require('worker_threads ').threadId`]: #worker_threads_worker_threadid
481485[`cluster` module]: cluster.html
482486[`inspector`]: inspector.html
483487[v8.serdes]: v8.html#v8_serialization_api
0 commit comments