[英]TypeError: util.promisify is not a function?
I'm trying to promisify zlib.gunzip
in my react application: 我正在尝试在我的react应用程序中承诺zlib.gunzip
:
const zlib = require('zlib')
const util = require('util')
const gunzipPromisified = util.promisify(zlib.gunzip)
But I get this error: 但是我得到这个错误:
TypeError: util.promisify is not a function TypeError:util.promisify不是一个函数
That works fine if I put it in a standalone script file and run it through node
. 如果我将其放在一个独立的脚本文件中并通过node
运行它,那会很好。
If I try: 如果我尝试:
import zlib from 'zlib'
import util from 'util'
const gunzipPromisified = util.promisify(zlib.gunzip)
I get something even fancier: 我得到了一些更奇特的东西:
TypeError: __WEBPACK_IMPORTED_MODULE_8_util___default.a.promisify is not a function TypeError:__WEBPACK_IMPORTED_MODULE_8_util ___ default.a.promisify不是一个函数
What am I missing? 我想念什么?
[Edit] The node.js version installed on my laptop is 8.9.1. [编辑]我的笔记本电脑上安装的node.js版本是8.9.1。
[Edit] As somebody commented, the node.js installed in my local development environment has nothing to do with what the app code has access to in the browser. [编辑]有人评论说,安装在我本地开发环境中的node.js与应用程序代码在浏览器中可以访问的内容无关。 So now my question is, how do I determine what API I have access to in the browser? 所以现在我的问题是,如何确定我可以在浏览器中访问的API?
I had the same problem, it got fixed by updating Node to a version higher than Node 7. 我遇到了同样的问题,它通过将Node更新到高于Node 7的版本来解决。
n latest
In my case Node 9.2.0 as this was added later. 在我的情况下,Node 9.2.0是后来添加的。
I had the same problem in Ionic. 我在Ionic遇到了同样的问题。 The util promisify didn't work for me either, and generated the same error as the one for OP. util promisify对我也不起作用,并且产生了与OP相同的错误。 I ended up using this simple solution copied below: 我最终使用了下面复制的这个简单解决方案 :
const promisify = (fn) => {
return (...args) => {
return new Promise((resolve, reject)=>{
fn(...args, function(err, res){
if(err){
return reject(err);
}
return resolve(res);
})
})
}
}
And then: 接着:
const requestPromisified = promisify(request)
当然,当我切换到bluebird的Promise
时,一切正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.