Welcome to the Linux Foundation Forum!

chapter 15, connect STDOUT to a fileStream, help.

theodoros
theodoros Posts: 16
edited February 7 in LFW211 Class Forum

hi, i m playing with the child_process,
and on spawn() command, i try to replace the stdio[1] with a writable stream e.g. fs.createWriteStream().

I m not sure if this is possible, i just thought that process.STDOUT is a writable stream too, so why not replace, so to get the output to a file!

The code is as simple as that:

const { spawn } = require('child_process')
const path = require('path')
const fs = require('fs')

const writableStream = fs.createWriteStream('out', { encoding: 'utf8' })

console.log(process.cwd())
console.log('i m running the spawn method')

const child = spawn(process.execPath, ['hello.js'], {
  stdio: ['pipe', writableStream, 'ignore'],  // this fails, because of stdio[1]
  cwd: 'london',
  env: { MY_VAR: 'theo'}
})

child.stdin.write('this is input which become output!')
child.stdin.end()

child.on('close', (code) => { console.log('exit code :', code) })

The error that i get is a TypeError, like so: TypeError [ERR_INVALID_ARG_VALUE]: The argument 'stdio' is invalid. Received WriteStream

Has anyone tried a similar approach ? thanks. :)

Comments

  • @theodoros take a look at point 6 under https://nodejs.org/dist/latest-v16.x/docs/api/child_process.html#optionsstdio

    The stream must have an underlying descriptor (file streams do not until the 'open' event has occurred).

    You need to listen for the open event on the write stream first, so that Node can internally extract the file handle and inject into the child process.

  • For anyone that want the response, below two approaches to solve the same issue:

    const {spawn} = require('child_process')
    const path = require('path')
    const fs = require('fs')
    const {once} = require('stream')
    
    const writableStream = fs.createWriteStream('out-sample.txt', {encoding: 'utf8'});
    
    writableStream.on('open', () => {
        console.log(process.cwd())
        console.log('i m running the spawn method')
    
        const child = spawn(process.execPath, ['hello.js'], {
            stdio: ['pipe', writableStream, 'ignore'],
            env: {MY_VAR: 'theo'}
        })
    
        child.stdin.write('this is input which become output!')
        child.stdin.end()
    
        child.on('close', (code) => {
            console.log('exit code :', code)
        })
    })
    
    async function run() {
        const writableStream = fs.createWriteStream('out-sample.txt', {encoding: 'utf8'});
    
        await once(writableStream, 'open');
    
        console.log(process.cwd())
        console.log('i m running the spawn method')
    
        const child = spawn(process.execPath, ['hello.js'], {
            stdio: ['pipe', writableStream, 'ignore'],
            env: {MY_VAR: 'theo'}
        })
    
        child.stdin.write('this is input which become output!')
        child.stdin.end()
    
        child.on('close', (code) => {
            console.log('exit code :', code)
        })
    }
    run().catch(console.error);
    
    

Categories

Upcoming Training