Answers for "bash commands in node"

9

node execute shell commands

const { exec } = require("child_process");
exec("cat index.js", (error, data, getter) => {
	if(error){
		console.log("error",error.message);
		return;
	}
	if(getter){
		console.log("data",data);
		return;
	}
	console.log("data",data);

});
Posted by: Guest on April-11-2020
1

execute terminal command nodejs

// You can use 'exec' this way
// LINUX EXAMPLE

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}`);
});
Posted by: Guest on October-12-2020
0

bash commands in node

const { exec } = require('child_process');
exec('ls | grep js', (err, stdout, stderr) => {  
  if (err) {    
    //some err occurred    
    console.error(err)  
  } else {   
    // the *entire* stdout and stderr (buffered)   
    console.log(`stdout: ${stdout}`);   
    console.log(`stderr: ${stderr}`);  
  }
});
Posted by: Guest on July-06-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language