1// You can use 'exec' this way
2
3const { exec } = require("child_process");
4
5exec("ls -la", (error, stdout, stderr) => {
6 if (error) {
7 console.log(`error: ${error.message}`);
8 return;
9 }
10 if (stderr) {
11 console.log(`stderr: ${stderr}`);
12 return;
13 }
14 console.log(`stdout: ${stdout}`);
15});
16
1const { exec } = require("child_process");
2exec("cat index.js", (error, data, getter) => {
3 if(error){
4 console.log("error",error.message);
5 return;
6 }
7 if(getter){
8 console.log("data",data);
9 return;
10 }
11 console.log("data",data);
12
13});
14
1// You can use 'exec' this way
2// LINUX EXAMPLE
3
4const { exec } = require("child_process");
5
6exec("ls -la", (error, stdout, stderr) => {
7 if (error) {
8 console.log(`error: ${error.message}`);
9 return;
10 }
11 if (stderr) {
12 console.log(`stderr: ${stderr}`);
13 return;
14 }
15 console.log(`stdout: ${stdout}`);
16});
1// Run a command asynchronously
2const { spawn } = require('child_process');
3const dir = spawn('cmd', ['/c', 'dir']);
4
5dir.stdout.on('data', data => console.log(`Stdout: ${data}`));
6dir.stderr.on('data', data => console.log(`Stderr: ${data}`));
7dir.on('close', code => console.log(`Exit code: ${code}`));
8
9// Run a command synchronously
10const { spawnSync } = require( 'child_process' );
11const dir = spawnSync('cmd', ['/c', 'dir']);
12
13console.log(`Stdout: ${dir.stdout.toString()}`);
14console.log(`Stderr: ${dir.stderr.toString()}`);