Answers for "how to run a file in node"

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
0

nodejs open file

fs = require('fs')
fs.readFile('/etc/hosts', 'utf8', function (err,data) {
  if (err) {
    return console.log(err);
  }
  console.log(data);
});
Posted by: Guest on December-05-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language