child_process.exec
3 July 2025 (Updated 3 July 2025)
The child_process.exec
method spawns a new shell and executes the given command
inside it.
The command
string passed to the exec
function will be processed directly by the operating system shell which means it’s up to you to ensure you handle shell-specific nuances like special characters.
Here’s an example usage:
import child_process from 'node:child_process'
import util from 'node:util'
const exec = util.promisify(child_process.exec)
async function main() {
try {
const { stdout, stderr } = await exec('ls -la')
console.log({ stdout, stderr })
} catch (error) {
console.error({ error })
}
}
main()
Tagged:
Node.js