☕️ 1 min read

How to run command line by nodejs

avatar
Milad E. Fahmy
@miladezzat12

How to run command line by nodejs

const util = require('util')
const exec = util.promisify(require('child_process').exec)

async function processChild() {
  try {
    const { stdout, stderr } = await exec('ls')
    if (stdout) {
      console.log('stdout: ', stdout)
    }

    if (stderr) {
      console.log('stderr:', stderr)
    }
  } catch (e) {
    console.error(e)
  }
}

processChild()

Note: https://nodejs.org/api/child_process.html