有可能在 node 中调用一个 function,但控制 function 的输出流(console.log)吗?

2020-03-03 22:08:52 +08:00
 vevlins

我要调用另外一个包,这个包有个函数内部有 console.log 。

只知道调系统命令时可以控制 stdout,node 中有办法做到吗?

3187 次点击
所在节点    Node.js
3 条回复
Buges
2020-03-03 22:17:10 +08:00
直接 override console.log 函数不行吗?
zivyou
2020-03-03 22:59:49 +08:00
1l 标准答案
himself65
2020-03-11 21:08:03 +08:00
```js
const fn = console.log
console.log = () => {}
require('xxx.js')
console.log = fn
```

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/649580

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX