使用节点npm模块(https://www.npmjs.org/api/npm.html),我想检索一个包,但没有将结果打印到控制台。设置loglevel silent似乎没有帮助。
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!
});
});
答案 0 :(得分:2)
如果没有劫持stdout
,就无法做到这一点。请参阅:https://github.com/npm/npm/blob/master/lib/install.js#L100
这已修复一次,但随后又恢复了。似乎npm
未设计为以编程方式使用。
通过“劫持stdout
”我的意思是这样的:
console.log('Begin');
console._stdout = { write : function () {} };
externalFn();
console._stdout = process.stdout;
console.log('End');
function externalFn () {
console.log('Annoying stuff');
}