Execute a Command Line Binary With Node.Js

Run a binary from Node.JS and relay the behavior exactly on the console, including prompts

You can use 'inherit' for the stdio option of spawn:

const spawn = require( 'child_process' ).spawn;
spawn( '/path/to/binary', [], { stdio: 'inherit' } );

Execute and get the output of a shell command in node.js

This is the method I'm using in a project I am currently working on.

var exec = require('child_process').exec;
function execute(command, callback){
exec(command, function(error, stdout, stderr){ callback(stdout); });
};

Example of retrieving a git user:

module.exports.getGitUser = function(callback){
execute("git config --global user.name", function(name){
execute("git config --global user.email", function(email){
callback({ name: name.replace("\n", ""), email: email.replace("\n", "") });
});
});
};

How to execute node command in a node app?

run it sequentially with execSync

const execSync = require('child_process').execSync;

//...
switch (arg) {
case "React App":
console.log(`npx create-react-app ${projectName}`);
execSync(`npx create-react-app ${projectName}`);
console.log(`cd ${projectName}`);
execSync(`cd ${projectName}`);
console.log(`npm start`);
execSync(`npm start`);
break;
case "Next App":
console.log(`npx create-next-app@latest ${projectName}`);
execSync(`npx create-next-app@latest ${projectName}`);
console.log(`cd ${projectName}`);
execSync(`cd ${projectName}`);
console.log(`npm dev`);
execSync(`npm dev`);
break;
}

Execute bash script via Node.js and include command line parameters

The second parameter in spawn is an array of arguments to pass to the command. So I think you almost have it but instead of concating the params to the path pass them in as an array:

var spawn = require('child_process').spawn;
var params = ['pathToScript','run', '-silent'];
spawn('bash', params, {
stdio: 'ignore',
detached: true
}).unref();


Related Topics



Leave a reply



Submit