child_process.spawn throws EINVAL on Windows after a Node.js update
Error: spawn EINVALShort 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.
Fix it fast
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);2Match what you see, then jump to its fix
What does the error look like?
Error: spawn EINVALcode: '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 *.ts2Fix
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
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 *.ts2Fix
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
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 fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack 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 fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix