childProcess.stdout.on("data") does not get progress output from npm install
- 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.
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
There is no callback arg on spawn so it can't be used with the Promisify util to return a promise.
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 }`);
});
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.
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.