Quick actions

cmd+k|ctrl+k

Navigation

Languages

child_process stdin

Snippet info

Language

JavaScript

Visibility

public

Author

homam

Created

2018-02-13T09:18:19Z

Updated

2018-02-13T11:43:25Z

const { spawn } = require('child_process');

const child = spawn('wc');
child.stdin.setEncoding('utf-8');
child.stdin.write("console.log('Hello world')\n");
child.stdin.end()

child.stdout.on('data', data =>
    console.log(`child stdout:\n${data}`)
)

child.on('close', code => 
    console.log('Closed', code)
)

// or make a simple re-usable module
const run = require('./run')
run('wc', "console.log('Hello world')")
.then(console.log)
.catch(console.log)
INFO