0

I am looking for a clear answer on how the stdout and stderr work when developing a npm cli module.

I would like to print out everything exactly as it is when I run command with child_process.spawn.

I managed to output the git clone command with the --progress options. Now I want to output the npm install command but it is printing only the final string.

Would be nice to have a clear answer on how this work in general and what are the best practices.

This works:

import * as cp from 'child_process';

const child = cp.spawn('git', ['clone', 'ssh://myrepo...', '--progress']);
    
child.stdout.setEncoding('utf8');
child.stdout.on('data', (chunk) => {
    process.stdout.write(`${chunk}`);
});
    
child.stderr.setEncoding('utf8');
child.stderr.on('data', (chunk) => {
    process.stdout.write(`${chunk}`);
});

// BTW why git is outputing in STDERR and not in STDOUT?

However this doesn't work

const child = cp.spawn('npm', ['i', 'mymodule']);

Is there a way to make this work for each command?

ndr4
  • 107
  • 8

1 Answers1

1

You can leave off the event handlers and encoding settings and tell spawn() to pass stdio through to the parent process.

const child = cp.spawn('npm', ['i', 'mymodule'], {stdio: 'inherit'});

Both of your commands work for me as long as the listeners are added as in your first example. Your second example only stops working for me if I leave out the event handlers. It would be interesting to know what platform you are running on, what versions of node and npm you are using, and a complete not-working example that can be cut and paste into a file for testing. (Also, what is the name of the file? Are you using .mjs extension or something else to make import work? Is this file in a module loaded elsewhere or in the base level script?)

Trott
  • 52,114
  • 21
  • 134
  • 179
  • I am using Typescript (tsc v3.9.5). That is why I use `import`. Node v14.2.0. Linux elementary OS 5.1.7 Hera. Filename is the compiled `cli.js` from `cli.ts` with `#!/usr/bin/env node`. – ndr4 Feb 24 '21 at 14:52
  • Can it be that the output of `npm` command doesn't show because is formatted in some other way? I am noticing that what I don't see is colored when I use `{stdio: 'inherit'}` – ndr4 Feb 24 '21 at 15:41