help icon indicating copy to clipboard operation
help copied to clipboard

childProcess.stdout.on("data") does not get progress output from npm install

Open Roaders opened this issue 4 years ago • 4 comments

  • Node.js Version: 12.18.0
  • OS: Windows 10
  • Scope (install, code, runtime, meta, other?): runtime
  • Module (and version) (if relevant): child_process
const installProcess = exec(`npm install lodash`);

installProcess.stdout.on('data', data => console.log(`stdout:data`, data));

output:

stdout:data + [email protected]
removed 1 package and updated 1 package in 18.399s

stdout:data
86 packages are looking for funding
  run `npm fund` for details

when you do npm install on the command line you get a load of output indicating the progress being made. I want to show this progress but I do not get any events from installProcess.stdout.on('data') other than the output at the end when the install has finished. I have tried as many different events as I can find (including readable and this.read()) and I have tried spawn rather than exec but still don't seem to get any progress updates.

I assume that npm is logging these events to a different stream but I don't seem to be able to intercept it at all.

Is there a way of listening to these updates?

Many thanks.

Roaders avatar Feb 06 '21 10:02 Roaders

How about this?

test.js

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

const installProcess = spawn('npm', ['install', 'lodash'], {
  stdio: 'inherit'
});

installProcess.on('close', (code) => console.log(`exit code: ${ code }`));
❯ node test.js

up to date, audited 1 package in 2s

found 0 vulnerabilities
exit code: 0

RaisinTen avatar Feb 06 '21 16:02 RaisinTen

There is no callback arg on spawn so it can't be used with the Promisify util to return a promise.

Roaders avatar Feb 07 '21 09:02 Roaders

We can write a wrapper around spawn to accept a callback. Here is a very basic implementation of what I mean:

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

function mySpawn(command, args, options, cb) {
  const child = spawn(command, args, options);
  child.on('close', (exitCode) => { cb(null, exitCode) });
  return child;
}

const mySpawnPromisified = util.promisify(mySpawn);

mySpawnPromisified(
  'npm',
  ['i', 'lodash'],
  { stdio: 'inherit' }
).then(function(exitCode) {
    console.log(`Exited with: ${ exitCode }`);
  });

RaisinTen avatar Feb 08 '21 16:02 RaisinTen

It seems there has been no activity on this issue for a while, and it is being closed in 30 days. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.

github-actions[bot] avatar May 10 '24 01:05 github-actions[bot]

It seems there has been no activity on this issue for a while, and it is being closed. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.

github-actions[bot] avatar Jun 09 '24 01:06 github-actions[bot]