Skip to content

node:worker_threads does not load --import file.js unless explicitly setting env option #62301

@AriPerkkio

Description

@AriPerkkio

Version

v22.22.0

Platform

Darwin Aris-MacBook-Air.local 25.3.0 Darwin Kernel Version 25.3.0: Wed Jan 28 20:56:34 PST 2026; root:xnu-12377.91.3~2/RELEASE_ARM64_T8112 arm64

Subsystem

No response

What steps will reproduce the bug?

Create file worker-repro.mjswith following:

import { writeFileSync } from "node:fs";
import { Worker, isMainThread } from "node:worker_threads";

if (isMainThread) {
  process.env.NODE_OPTIONS ||= "";
  process.env.NODE_OPTIONS += " --import ./worker-loader.mjs";
  process.env.HELLO_WORLD = "Hello, World!";

  writeFileSync(
    "./worker-loader.mjs",
    `console.log("[loader]", { pid: process.pid });`,
  );

  console.log("[main]", { pid: process.pid });
  new Worker("./worker-repro.mjs");
}

if (!isMainThread) {
  console.log("[worker]", {
    pid: process.pid,
    NODE_OPTIONS: process.env.NODE_OPTIONS,
    HELLO_WORLD: process.env.HELLO_WORLD,
  });
}

How often does it reproduce? Is there a required condition?

Always

What is the expected behavior? Why is that the expected behavior?

$ node worker-repro.mjs 
[main] { pid: 44824 }
[loader] { pid: 44824 }
[worker] {
  pid: 44824,
  NODE_OPTIONS: ' --import ./worker-loader.mjs',
  HELLO_WORLD: 'Hello, World!'
}

I expect [loader] { pid: 44824 } to be logged. The file passed via --import should be loaded before [worker] context is loaded.

With following change the loader is loaded as expected:

- new Worker("./worker-repro.mjs");
+ new Worker("./worker-repro.mjs", { env: process.env });

But this change should not be needed. Even without this change we can see that [worker] is logging same NODE_OPTIONS and HELLO_WORLD environment variables that parent set.

What do you see instead?

--import has no effect, there is no [loader] { pid: 44824 } logged.

$ node worker-repro.mjs 
[main] { pid: 44790 }
[worker] {
  pid: 44790,
  NODE_OPTIONS: ' --import ./worker-loader.mjs',
  HELLO_WORLD: 'Hello, World!'
}

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions