new Worker(filename[, options])


  • filename <string> | <URL> 工作线程的主脚本或模块的路径。 必须是以 ./../ 开头的绝对路径或相对路径(即相对于当前工作目录)、或者是使用 file:data: 协议的 WHATWG URL 对象 当使用 data: 网址时,使用 ECMAScript 模块加载器根据 MIME 类型解释数据。 如果 options.evaltrue,则这是包含 JavaScript 代码(而不是路径)的字符串。
  • options <Object>
    • argv <any[]> 将被字符串化并附加到工作线程中的 process.argv 的参数列表。 这与 workerData 非常相似,但这些值在全局的 process.argv 上可用,就好像它们作为 CLI 选项传给脚本一样。
    • env <Object> 如果设置,则指定工作线程内 process.env 的初始值。 作为特殊值,worker.SHARE_ENV 可以用来指定父线程和子线程应该共享它们的环境变量;在这种情况下,对线程的 process.env 对象的更改也会影响另一个线程。 默认值: process.env
    • eval <boolean> 如果 true 并且第一个参数是 string,则将构造函数的第一个参数解释为一旦工作线程在线就执行的脚本。
    • execArgv <string[]> 传给工作线程的 node CLI 选项的列表。 不支持 V8 选项(如 --max-old-space-size)和影响进程的选项(如 --title)。 如果设置,则此在工作线程内部作为 process.execArgv 提供。 默认情况下,选项继承自父线程。
    • stdin <boolean> 如果设置为 true,则 worker.stdin 提供其内容在工作线程中显示为 process.stdin 的可写流。 默认情况下,不提供任何数据。
    • stdout <boolean> 如果设置为 true,则 worker.stdout 不会自动通过管道传输到父线程中的 process.stdout
    • stderr <boolean> 如果设置为 true,则 worker.stderr 不会自动通过管道传输到父线程中的 process.stderr
    • workerData <any> 任何被克隆并作为 require('node:worker_threads').workerData 可用的 JavaScript 值。 克隆按照 HTML 结构化克隆算法中的描述进行,如果无法克隆对象(例如,因为它包含 functions),则会抛出错误。
    • trackUnmanagedFds <boolean> 如果设置为 true,则工作线程会跟踪通过 fs.open()fs.close() 管理的原始文件描述符,并在工作线程退出时关闭它们,类似于网络套接字或通过 FileHandle API 管理的文件描述符等其他资源。 此选项会被所有嵌套的 Worker 自动继承。 默认值: true
    • transferList <Object[]> 如果在 workerData 中传入一个或多个类似 MessagePort 的对象,则这些条目需要 transferList 或抛出 ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST。 有关详细信息,请参阅 port.postMessage()
    • resourceLimits <Object> 新的 JS 引擎实例的一组可选资源限制。 达到这些限制会导致 Worker 实例终止。 这些限制只影响 JS 引擎,不影响外部数据,不包括 ArrayBuffer。 即使设置了这些限制,如果遇到全局内存不足的情况,进程仍可能会中止。
      • maxOldGenerationSizeMb <number> 主堆的最大大小 (以 MB 为单位)。 如果设置了命令行参数 --max-old-space-size,它将覆盖此设置。
      • maxYoungGenerationSizeMb <number> 最近创建的对象的最大堆空间大小。 如果设置了命令行参数 --max-semi-space-size,它将覆盖此设置。
      • codeRangeSizeMb <number> 用于生成代码的预分配内存范围的大小。
      • stackSizeMb <number> 线程的默认最大堆栈大小。 较小的值可能会导致工作线程实例无法使用。 默认值: 4
  • filename <string> | <URL> The path to the Worker's main script or module. Must be either an absolute path or a relative path (i.e. relative to the current working directory) starting with ./ or ../, or a WHATWG URL object using file: or data: protocol. When using a data: URL, the data is interpreted based on MIME type using the ECMAScript module loader. If options.eval is true, this is a string containing JavaScript code rather than a path.
  • options <Object>
    • argv <any[]> List of arguments which would be stringified and appended to process.argv in the worker. This is mostly similar to the workerData but the values are available on the global process.argv as if they were passed as CLI options to the script.
    • env <Object> If set, specifies the initial value of process.env inside the Worker thread. As a special value, worker.SHARE_ENV may be used to specify that the parent thread and the child thread should share their environment variables; in that case, changes to one thread's process.env object affect the other thread as well. Default: process.env.
    • eval <boolean> If true and the first argument is a string, interpret the first argument to the constructor as a script that is executed once the worker is online.
    • execArgv <string[]> List of node CLI options passed to the worker. V8 options (such as --max-old-space-size) and options that affect the process (such as --title) are not supported. If set, this is provided as process.execArgv inside the worker. By default, options are inherited from the parent thread.
    • stdin <boolean> If this is set to true, then worker.stdin provides a writable stream whose contents appear as process.stdin inside the Worker. By default, no data is provided.
    • stdout <boolean> If this is set to true, then worker.stdout is not automatically piped through to process.stdout in the parent.
    • stderr <boolean> If this is set to true, then worker.stderr is not automatically piped through to process.stderr in the parent.
    • workerData <any> Any JavaScript value that is cloned and made available as require('node:worker_threads').workerData. The cloning occurs as described in the HTML structured clone algorithm, and an error is thrown if the object cannot be cloned (e.g. because it contains functions).
    • trackUnmanagedFds <boolean> If this is set to true, then the Worker tracks raw file descriptors managed through fs.open() and fs.close(), and closes them when the Worker exits, similar to other resources like network sockets or file descriptors managed through the FileHandle API. This option is automatically inherited by all nested Workers. Default: true.
    • transferList <Object[]> If one or more MessagePort-like objects are passed in workerData, a transferList is required for those items or ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST is thrown. See port.postMessage() for more information.
    • resourceLimits <Object> An optional set of resource limits for the new JS engine instance. Reaching these limits leads to termination of the Worker instance. These limits only affect the JS engine, and no external data, including no ArrayBuffers. Even if these limits are set, the process may still abort if it encounters a global out-of-memory situation.
      • maxOldGenerationSizeMb <number> The maximum size of the main heap in MB. If the command-line argument --max-old-space-size is set, it overrides this setting.
      • maxYoungGenerationSizeMb <number> The maximum size of a heap space for recently created objects. If the command-line argument --max-semi-space-size is set, it overrides this setting.
      • codeRangeSizeMb <number> The size of a pre-allocated memory range used for generated code.
      • stackSizeMb <number> The default maximum stack size for the thread. Small values may lead to unusable Worker instances. Default: 4.