I have a fresh installation of Ubuntu 20.04.2 (run in a virtual machine on mac).
I've installed Node.js v16 (via official software channel) and observed an extremely weird issue. Child processes spawned with studio: 'pipe' do not emit any stdout or stderr data.
Steps to reproduce:
child.js
console.log("Some text");
const interval = setInterval(() => {
console.log("Other text")
}, 100)
setTimeout(() => clearInterval(interval), 1000);
main.js
const childProcess = require('child_process');
const child = childProcess.spawn('node', ['child.js'], { stdio: 'pipe'});
if (child.stdout) {
console.log("Attach std listeners")
child.stdout.on('data', data => console.log("Child stdout:", String(data)));
child.stderr.on('data', data => console.log("Child stderr:", String(data)));
}
child.on('close', (...args) => { console.log("Child closed", args); });
Running node test.js produces following in Ubuntu terminal for me:
Attach std listeners
CLOSED [ 0, null ]
(when I run it on macOS, I get as expected multiple Child stdout: Other text in output)
While if I change stdio to inherit in main script, the output from child process is exposed:
Some text
Other text
Other text
Other text
Other text
Other text
Other text
Other text
Other text
Other text
Child closed [ 0, null ]
It signals to me that the child process is run without issues but for some reason, no stdout and stderr are emitted by a child when its output is supposed to be piped.
The same issue occurs with other Node.js versions I've checked (v14, v17, v18nightly)
It appears the problem is when Node.js is installed via Snap dependency manager (so official Ubuntu Software installer).
Software is then sandboxed in read-only containers, with restricted access to other system parts, and as it shows that cannot work for Node.js
Solution: Do not install Node.js via Snap, install via apt instead