Repository navigation
Stop throwing for unsupported options in Worker's execArgv and env.NODE_OPTIONS. #41103
Description
Version
v17.2.0
Platform
No response
Subsystem
worker_threads
What steps will reproduce the bug?
Consider this test.js file:
const { isMainThread, Worker } = require("worker_threads");
if (isMainThread) {
new Worker(__filename, {
// execArgv: process.execArgv
});
} else {
console.log("Hello from the worker!", process.execArgv);
}NOTE:
--experimental-json-modulesand--titleare just examples
Run these two commands:
node --experimental-json-modules test.js
node --experimental-json-modules --title foo test.js
They will log
Hello from the worker! [ '--experimental-json-modules' ]
Hello from the worker! [ '--experimental-json-modules', '--title', 'foo' ]
Now, uncomment the execArgv: process.execArgv line and run the two commands again. The first command will still log
Hello from the worker! [ '--experimental-json-modules' ]
however, the second one throws:
Error [ERR_WORKER_INVALID_EXEC_ARGV]: Initiated Worker with invalid execArgv flags: --title
at new NodeError (node:internal/errors:371:5)
at new Worker (node:internal/worker:191:13)
at Object.<anonymous> (/home/nicolo/Documenti/dev/babel/babel/test.js:4:3)
at Module._compile (node:internal/modules/cjs/loader:1097:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:17:47 {
code: 'ERR_WORKER_INVALID_EXEC_ARGV'
}
This mans that the default behavior of the Worker execArgv option is not to inherit execArgv from the parent thread (contrary to what the documentation says).
Why would you want to explicitly pass execArgv: process.execArgv anyway?
A few days ago I was trying to implement a require hook that internally uses a Worker, and in order to avoid registering it recursively in an infinite worker-spawn loop I tried to filter out the --require flag:
new Worker(workerFile, {
execArgv: process.execArgv.filter((val, i, argv) => val === "--requrie" || argv[i-1] === "--requrie"),
});this seemed to work, until it crashed in a test that was using the --expose_gc option.
Today, I found the exact same problem in jest-worker (a Jest subpackage that is often also used outside of Jest): jestjs/jest#12103 was opened because jest-worker didn't work when running Node.js with --max_old_space_size.
Since Node.js doesn't provide the list of flags supported by workers progrmmatically (and I didn't find it in the docs either 😅) there is no way to filter the execArgv used by a worker.
What is the current behavior?
I initially expected the real default behavior to be something like execArgv: process.execArgv.filter(isSupportedByWorker), but it looks like it's not either. Consider for example the --expose_gc (which is disallowed in workers) option with this example:
const { isMainThread, Worker } = require("worker_threads");
if (isMainThread) {
new Worker(__filename);
} else {
console.log("Hello from the worker!", process.execArgv, globalThis.gc);
}If you run node --expose_gc test.js, it will log
Hello from the worker! [ '--expose_gc' ] [Function: gc]
You can see that the gc function is present. This means that the disallowed flags are not ignored (and not even "ignored but still used to populate the process.execArgv array): they are used and affect the runtime behavior.
How often does it reproduce? Is there a required condition?
Only for the options marked as "unsupported" in workers.
What is the expected behavior?
I see four possible solutions:
- Just ignore the unsupported options
- Never throw, but warn when passing an unsupported option to a worker (potentially even if it's passed by default without an explicit
execArgv). - Only throw if the unsupported option is not also used as an
execArgvof the parent thread. - Always throw for unsupported options (so that
execArgv: process.execArgvbecomes the default behavior), and provide aWorker.filterExecArgvutility that removes disallowed options, and that I would have used likeWorker.filterExecArgv(process.execArgv).filter((val, i, argv) => val === "--requrie" || argv[i-1] === "--requrie")
If the current behavior is expected, then the docs should be updated to mention that the default value cannot be replicated by explicitly passing an execArgv option.
What do you see instead?
No response
Additional information
No response
Activity
targos commentedon Dec 7, 2021
@nodejs/workers
[-]Stop throwing for unsupported options in `Worker`'s `execArgv`.[/-][+]Stop throwing for unsupported options in `Worker`'s `execArgv` and `env.NODE_OPTIONS`.[/+]nicolo-ribaudo commentedon Dec 11, 2021
The same happens with
env.When running
NODE_OPTIONS=--openssl-legacy-provider node test.jswith Node.js 17.2.0, this works:but this throws
meixg commentedon Feb 28, 2022
This may be not right, as:
run
node --expose_gc test.jswill log:Seems that this behavior is not controlled by work's
execArgv.meixg commentedon Feb 28, 2022
If
execArgvis not specified, all parent's args indeed are used:node/src/node_worker.cc
Lines 551 to 553 in 4cb2a47
node/src/node_worker.cc
Lines 308 to 315 in 4cb2a47
But since these Per-Process options and V8 flags are from parents, I guess it is ok?
#25467
Remove --title from certain executables
jimmywarting commentedon May 9, 2023
this have for sure been bugging me too, some flags gets added or removed in different versions.
one needs to be able to figure out what flags are supported or not before you can pass this execArgv commands
shellscape commentedon Jun 5, 2023
Just ran into this using
avaavajs/ava#3207aaronovz1 commentedon Jul 19, 2023
What's the word on this? Just hit this on taskforcesh/bullmq#2075 which is a pain because without a change to that package, worker_threads can't be used.
bcomnes commentedon May 24, 2024
+1 for Ignore the unsupported options
joyeecheung commentedon Jun 26, 2024
Opened #53596 for the simpler case of NODE_OPTIONS, when the worker spawning code does not intend to modify NODE_OPTIONS and only copies the one from the parent because they want to update other environment variables.
worker: allow copied NODE_OPTIONS in the env setting
worker: allow copied NODE_OPTIONS in the env setting
kirklimbu commentedon Jul 23, 2024
I came across this error while building angular 18 app. I solved it by disabling SSR "prerender": false, "ssr": false inside project.json file
alirezaraha3 commentedon Aug 8, 2024
Game_spiking @dance _
everett1992 commentedon Sep 9, 2024
I ran into this when building an old project that uses mocha-parallel-test and node's --openssl-legacy-provider option.
mocha-parallel-tests spawns workers and specifies execArgv to remove some 'debug' flags from the process execArgv. But it doesn't remove openssl-legacy-provider so node throws
Error [ERR_WORKER_INVALID_EXEC_ARGV]: Initiated Worker with invalid execArgv flags: --openssl-legacy-providerhttps://github.com/mocha-parallel/mocha-parallel-tests/blob/master/src/main/thread/worker.ts#L35-L36
Either node should ignore these options by default, as it appears to when using the default execArgv, or node should provide a function to remove unsupported options, or return
process.execArgvwithout per-process arguments.github-actions commentedon Jun 25, 2026
This issue has been marked as stale due to 210 days of inactivity.
It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.
jendrikw commentedon Jun 25, 2026
bump
github-actions commentedon Sep 25, 2026
This issue has been marked as stale due to 90 days of inactivity.
It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.
bcomnes commentedon Sep 25, 2026
Fairly sure this is still an issue.