Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

child_process.spawn throws EINVAL on Windows after a Node.js update

ErrorError: spawn EINVAL

Short answer

Since the April 2024 security releases, Node.js on Windows refuses to run a .bat or .cmd file through spawn or spawnSync unless the shell option is set. The call fails with EINVAL. Pass shell: true with sanitized input, or start the file through exec or cmd.exe /c.

  • Applies to Node.js 22, Node.js 24, Windows 11
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the exact command you pass to spawn. If it ends in .cmd or .bat, the cause below applies.

    // log it right before the spawn call
    console.log(command, args);
  2. 2Match what you see, then jump to its fix

    1. 1Error: spawn EINVAL, only on Windows, only for a command that ends in .cmd or .batA .cmd or .bat file is passed to spawn without the shell optionFix
    2. 2The arguments come from a request, a form or a file nameThe script takes values you do not controlFix

What does the error look like?

  • Error: spawn EINVAL
  • code: 'EINVAL'

Cause 1 of 2

A .cmd or .bat file is passed to spawn without the shell option

1Check

The Node.js security release for CVE-2024-27980 says Node.js now errors with EINVAL when a .bat or .cmd file goes to spawn or spawnSync without shell set. Find the call.

findstr /S /N /C:"spawn(" *.js *.ts

2Fix

If the arguments are sanitized (never raw user input), add shell: true. The release notes name this as the way to avoid EINVAL.

const { spawn } = require('node:child_process');

const child = spawn('build.cmd', ['--fast'], { shell: true });

3Verify

Run the same code again. The process should start and output should arrive.

child.stdout.on('data', (d) => console.log(String(d)));
child.on('exit', (code) => console.log('exit', code));

Confirmed by nodejs.org

Did this fix it?

Cause 2 of 2

The script takes values you do not control

1Check

The security release describes the original flaw: a malicious command line argument can inject commands when a batch file is started. With shell: true you must clean the input yourself.

findstr /S /N /C:"shell: true" *.js *.ts

2Fix

The child_process docs list other ways to run a .bat or .cmd file on Windows: exec, or spawning cmd.exe and passing the file as an argument. If the file name has spaces, quote it.

const { exec, spawn } = require('node:child_process');

exec('"my script.cmd" a b', (err, stdout, stderr) => {});

const bat = spawn('cmd.exe', ['/c', 'my.bat']);

3Verify

Check that the command runs and that a test argument with spaces arrives as one value.

node run-script.js "two words"

Confirmed by nodejs.org

Did this fix it?

How do I stop it coming back?

  • Keep a list of the .cmd and .bat files your tooling starts, and test those calls on Windows in CI.
  • Validate every argument against an allow-list before it reaches a shell.
  • Do not use the --security-revert flag; the Node.js team advises against it.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.