运行`npm install`时如何从node exec获取输出?

时间:2016-08-19 15:58:14

标签: node.js npm-install

我正在运行以下内容:

const exec = require('child_process').exec;
let installProcess = exec('npm install');
    installProcess.stdout.pipe(process.stdout);
    installProcess.stderr.pipe(process.stderr);

但我的终端没有输出,我还能尝试什么?

2 个答案:

答案 0 :(得分:1)

以下结果为我工作:

const execSync = require('child_process').execSync;
execSync('npm install', {stdio:[0,1,2]});

答案 1 :(得分:0)

这完美无缺

const exec = require('child_process').exec;
const installProcess = exec('npm install --verbose');

installProcess.stdout.on('data', process.stdout.write);
installProcess.stderr.on('data', process.stdout.write);
installProcess.on('close', (code) => process.stdout.write(`exited with ${code}`));

和结果

❯ node index.js   
stderr: npm
stderr:  info it worked if it ends with ok
npm verb cli [ '/usr/local/Cellar/node/6.3.0/bin/node',
npm verb cli   '/usr/local/bin/npm',
npm verb cli   'install',
npm verb cli   '--verbose' ]
npm info using npm@3.10.3
npm info using node@v6.3.0

stderr: npm verb 
stderr: correctMkdir /Users/bwin/.npm/_locks correctMkdir not in flight; initializing

stderr: npm
stderr:  info lifecycle tmp@1.0.0~preinstall: tmp@1.0.0

stderr: npm verb
stderr:  exit [ 0, true ]

stderr: npm info 
stderr: ok 

exited with 0