Created
April 14, 2020 08:57
-
-
Save alaztetik/c2eed85b9c8104181bc590ce8a164487 to your computer and use it in GitHub Desktop.
Executing Shell Commands with Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { exec } = require("child_process"); | |
exec("ls -la", (error, stdout, stderr) => { | |
if (error) { | |
console.log(`error: ${error.message}`); | |
return; | |
} | |
if (stderr) { | |
console.log(`stderr: ${stderr}`); | |
return; | |
} | |
console.log(`stdout: ${stdout}`); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { spawn } = require("child_process"); | |
const ls = spawn("ls", ["-la"]); | |
ls.stdout.on("data", data => { | |
console.log(`stdout: ${data}`); | |
}); | |
ls.stderr.on("data", data => { | |
console.log(`stderr: ${data}`); | |
}); | |
ls.on('error', (error) => { | |
console.log(`error: ${error.message}`); | |
}); | |
ls.on("close", code => { | |
console.log(`child process exited with code ${code}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment