worker_threads.SHARE_ENV
- 类型: <symbol>
一个特殊的值,可以作为 Worker 构造函数的 env 选项传入,用于指示当前线程和 Worker 线程应共享对同一组环境变量的读写访问权限。
【A special value that can be passed as the env option of the Worker
constructor, to indicate that the current thread and the Worker thread should
share read and write access to the same set of environment variables.】
import process from 'node:process';
import { Worker, SHARE_ENV } from 'node:worker_threads';
new Worker('process.env.SET_IN_WORKER = "foo"', { eval: true, env: SHARE_ENV })
.on('exit', () => {
console.log(process.env.SET_IN_WORKER); // Prints 'foo'.
});'use strict';
const { Worker, SHARE_ENV } = require('node:worker_threads');
new Worker('process.env.SET_IN_WORKER = "foo"', { eval: true, env: SHARE_ENV })
.on('exit', () => {
console.log(process.env.SET_IN_WORKER); // Prints 'foo'.
});