Answers for "node run command"

6

node run shell command

// You can use 'exec' this way

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 April-20-2020
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

node run command

// Run a command asynchronously
const { spawn } = require('child_process');
const dir = spawn('cmd', ['/c', 'dir']);

dir.stdout.on('data', data => console.log(`Stdout: ${data}`));
dir.stderr.on('data', data => console.log(`Stderr: ${data}`));
dir.on('close', code => console.log(`Exit code: ${code}`));

// Run a command synchronously
const { spawnSync } = require( 'child_process' );
const dir = spawnSync('cmd', ['/c', 'dir']);

console.log(`Stdout: ${dir.stdout.toString()}`);
console.log(`Stderr: ${dir.stderr.toString()}`);
Posted by: Guest on August-17-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language