Sometimes, we want to execute shell command in JavaScript.
In this article, we’ll look at how to execute shell command in JavaScript.
How to execute shell command in JavaScript?
To execute shell command in JavaScript, we can use child_process
module’s execSync
function.
For instance, we write
const { execSync } = require("child_process");
const output = execSync("ls", { encoding: "utf-8" });
console.log(output);
to call execSync
with 'ls'
and an object with the encoding
to run the ls
command and set the encoding of the output to Unicode.
Then we get the output
of the command returned.
Conclusion
To execute shell command in JavaScript, we can use child_process
module’s execSync
function.