@@ -125,6 +125,25 @@ if (isMainThread) {
125125}
126126```
127127
128+ ## worker.SHARE_ENV
129+ <!-- YAML
130+ added: REPLACEME
131+ -->
132+
133+ * {symbol}
134+
135+ A special value that can be passed as the ` env ` option of the [ ` Worker ` ] [ ]
136+ constructor, to indicate that the current thread and the Worker thread should
137+ share read and write access to the same set of environment variables.
138+
139+ ``` js
140+ const { Worker , SHARE_ENV } = require (' worker_threads' );
141+ new Worker (' process.env.SET_IN_WORKER = "foo"' , { eval: true , env: SHARE_ENV })
142+ .on (' exit' , () => {
143+ console .log (process .env .SET_IN_WORKER ); // Prints 'foo'.
144+ });
145+ ```
146+
128147## worker.threadId
129148<!-- YAML
130149added: v10.5.0
@@ -380,7 +399,11 @@ Notable differences inside a Worker environment are:
380399 and [ ` process.abort() ` ] [ ] is not available.
381400- [ ` process.chdir() ` ] [ ] and ` process ` methods that set group or user ids
382401 are not available.
383- - [ ` process.env ` ] [ ] is a read-only reference to the environment variables.
402+ - [ ` process.env ` ] [ ] is a copy of the parent thread's environment variables,
403+ unless otherwise specified. Changes to one copy will not be visible in other
404+ threads, and will not be visible to native add-ons (unless
405+ [ ` worker.SHARE_ENV ` ] [ ] has been passed as the ` env ` option to the
406+ [ ` Worker ` ] [ ] constructor).
384407- [ ` process.title ` ] [ ] cannot be modified.
385408- Signals will not be delivered through [ ` process.on('...') ` ] [ Signals events ] .
386409- Execution may stop at any point as a result of [ ` worker.terminate() ` ] [ ]
@@ -439,25 +462,30 @@ if (isMainThread) {
439462 If ` options.eval ` is ` true ` , this is a string containing JavaScript code
440463 rather than a path.
441464* ` options ` {Object}
465+ * ` env ` {Object} If set, specifies the initial value of ` process.env ` inside
466+ the Worker thread. As a special value, [ ` worker.SHARE_ENV ` ] [ ] may be used
467+ to specify that the parent thread and the child thread should share their
468+ environment variables; in that case, changes to one thread’s ` process.env `
469+ object will affect the other thread as well. ** Default:** ` process.env ` .
442470 * ` eval ` {boolean} If ` true ` , interpret the first argument to the constructor
443471 as a script that is executed once the worker is online.
444- * ` workerData ` {any} Any JavaScript value that will be cloned and made
445- available as [ ` require('worker_threads').workerData ` ] [ ] . The cloning will
446- occur as described in the [ HTML structured clone algorithm ] [ ] , and an error
447- will be thrown if the object cannot be cloned (e.g. because it contains
448- ` function ` s) .
472+ * ` execArgv ` {string [ ] } List of node CLI options passed to the worker.
473+ V8 options (such as ` --max-old-space-size ` ) and options that affect the
474+ process (such as ` --title ` ) are not supported. If set, this will be provided
475+ as [ ` process.execArgv ` ] [ ] inside the worker. By default, options will be
476+ inherited from the parent thread .
449477 * ` stdin ` {boolean} If this is set to ` true ` , then ` worker.stdin ` will
450478 provide a writable stream whose contents will appear as ` process.stdin `
451479 inside the Worker. By default, no data is provided.
452480 * ` stdout ` {boolean} If this is set to ` true ` , then ` worker.stdout ` will
453481 not automatically be piped through to ` process.stdout ` in the parent.
454482 * ` stderr ` {boolean} If this is set to ` true ` , then ` worker.stderr ` will
455483 not automatically be piped through to ` process.stderr ` in the parent.
456- * ` execArgv ` {string [ ] } List of node CLI options passed to the worker.
457- V8 options (such as ` --max-old-space-size ` ) and options that affect the
458- process (such as ` --title ` ) are not supported. If set, this will be provided
459- as [ ` process.execArgv ` ] [ ] inside the worker. By default, options will be
460- inherited from the parent thread .
484+ * ` workerData ` {any} Any JavaScript value that will be cloned and made
485+ available as [ ` require('worker_threads').workerData ` ] [ ] . The cloning will
486+ occur as described in the [ HTML structured clone algorithm ] [ ] , and an error
487+ will be thrown if the object cannot be cloned (e.g. because it contains
488+ ` function ` s) .
461489
462490### Event: 'error'
463491<!-- YAML
@@ -628,6 +656,7 @@ active handle in the event system. If the worker is already `unref()`ed calling
628656[ `vm` ] : vm.html
629657[ `worker.on('message')` ] : #worker_threads_event_message_1
630658[ `worker.postMessage()` ] : #worker_threads_worker_postmessage_value_transferlist
659+ [ `worker.SHARE_ENV` ] : #worker_threads_worker_share_env
631660[ `worker.terminate()` ] : #worker_threads_worker_terminate_callback
632661[ `worker.threadId` ] : #worker_threads_worker_threadid_1
633662[ Addons worker support ] : addons.html#addons_worker_support
0 commit comments