简体   繁体   English

检索包时阻止NodeJS npm打印详细信息

[英]Prevent NodeJS npm from printing details when retrieving package

Using the node npm module ( https://www.npmjs.org/api/npm.html ), I want to retrieve a package but NOT have it print the results to console. 使用节点npm模块( https://www.npmjs.org/api/npm.html ),我想检索一个程序包,但不希望它将结果打印到控制台。 Setting loglevel silent does not seem to help. 将日志级别设置为静默似乎没有帮助。

var npm = require('npm');
npm.load(function (er, npm) {
  // use the npm object, now that it's loaded.

  npm.config.set('loglevel', 'silent');

  npm.commands.view(["<package>@<version>"],function(err, npmBody) {
    //at this point it is always logging npmBody contents to console!
  });

});

This can't be done without hijacking stdout . 没有劫持stdout就不可能做到这一点。 See: https://github.com/npm/npm/blob/master/lib/install.js#L100 参见: https : //github.com/npm/npm/blob/master/lib/install.js#L100

This was fixed once, but then reverted. 此问题已修复一次,但随后又恢复了。 It appears that npm is not designed to be use programmatically. 看来npm并非旨在以编程方式使用。

By "hijacking stdout " I mean something like this: stdout “劫持stdout ”是这样的:

console.log('Begin');

console._stdout = { write : function () {} };

externalFn();

console._stdout = process.stdout;

console.log('End');



function externalFn () {
    console.log('Annoying stuff');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM