Node.js
Run a child command without a shell
Use execFile with an argument array when you need a child executable without shell interpolation. Keep the executable and arguments separate instead of building a command string from user input.
Worked example
Node.js example · save as example.mjs and run node example.mjs
import {execFile} from "node:child_process";
import {promisify} from "node:util";
const run = promisify(execFile);
const {stdout} = await run(process.execPath, ["-e", "console.log('child ready')"]);
console.log(stdout.trim());How it works
The example launches the current Node executable and captures its standard output. No shell interprets characters such as semicolons in the argument list. Promisify provides an awaitable interface that rejects on the child’s failure.
Limits and mistakes
Avoid enabling shell mode when arguments are untrusted. An executable can still interpret dangerous options or perform harmful actions even without a shell. ExecFile also buffers output; use spawn for large streaming output.
Check the result
Expect child ready. Test nonzero child exit status and verify that errors are handled. Keep the executable fixed or drawn from an explicit allowlist in restricted workflows.
Sources
Reference documentation for the behavior used in this guide: