On this page

    M

    worker_threads.getEnvironmentData

    History
    worker_threads.getEnvironmentData(key): any
    Attributes
    key:any
    Any arbitrary, cloneable JavaScript value that can be used as a Map key.
    Returns:any

    Within a worker thread, worker.getEnvironmentData() returns a clone of data passed to the spawning thread's worker.setEnvironmentData(). Every new Worker receives its own copy of the environment data automatically.

    import {
      Worker,
      isMainThread,
      setEnvironmentData,
      getEnvironmentData,
    } from 'node:worker_threads';
    
    if (isMainThread) {
      setEnvironmentData('Hello', 'World!');
      const worker = new Worker(new URL(import.meta.url));
    } else {
      console.log(getEnvironmentData('Hello'));  // Prints 'World!'.
    }
    const {
      Worker,
      isMainThread,
      setEnvironmentData,
      getEnvironmentData,
    } = require('node:worker_threads');
    
    if (isMainThread) {
      setEnvironmentData('Hello', 'World!');
      const worker = new Worker(__filename);
    } else {
      console.log(getEnvironmentData('Hello'));  // Prints 'World!'.
    }