Skip to content

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

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?

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

const isLoader = import.meta.url.endsWith("?loader=true");

if (isMainThread) {
  process.env.NODE_OPTIONS ||= "";
  process.env.NODE_OPTIONS += ` --import ${import.meta.filename}?loader=true`;
  process.env.HELLO_WORLD = "Hello, World!";

  console.log("[main]", { pid: process.pid });
  new Worker(import.meta.filename);
}

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

if (isLoader) {
  console.log("[loader]", { pid: process.pid });
}

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 /x/worker-import.mjs?loader=true',
  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(import.meta.filename);
+ new Worker(import.meta.filename, { 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 /x/worker-import.mjs?loader=true',
  HELLO_WORLD: 'Hello, World!'
}

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions