How to Execute a Shell Script in NodeJS
If you need to execute a shell script in NodeJS, then you can use the exec keyword.
Syntax: exec(command [, options] [, callback]
const shell = require('shelljs')
shell.exec("npm --version")
I use this for build scripts and automation tasks where Node orchestrates external tools. The shelljs library is a popular choice because it provides a synchronous API and works cross-platform — the same script runs on both Linux and Windows.
For the built-in approach without shelljs, you can use child_process.exec() or child_process.execSync() from Node’s standard library. The difference is that child_process gives you more control over stdout/stderr streams and exit codes, while shelljs is simpler for quick one-liners. For long-running processes, use spawn instead of exec to avoid buffering the entire output in memory.