简体   繁体   English

TypeError:util.promisify不是函数吗?

[英]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.

相关问题 NodeJs util.promisify 不是函数 - NodeJs util.promisify is not a function 如何通过writeFile fs函数使用节点js util.promisify并等待 - How to use node js util.promisify with the writeFile fs function and await 节点:util.promisify() 没有回调作为最终参数 - Node: util.promisify() without callback as final parameter 有没有办法在打字稿上使用 util.promisify 继承方法签名? - Is there any way to inherit method signature with util.promisify on typescript? 模块“util”已被外部化以实现浏览器兼容性。 无法在客户端代码中访问“util.promisify” - Module "util" has been externalized for browser compatibility. Cannot access "util.promisify" in client code 'util.promisify(setTimeout)' 和 'ms => new Promise(resolve => setTimeout(resolve, ms))' 之间的区别 - difference between 'util.promisify(setTimeout)' and 'ms => new Promise(resolve => setTimeout(resolve, ms))' Continue.map 循环后 util.promisify 请求迭代失败(异步等待) - Continue .map loop after util.promisify request fails in iteration (async await) Node.js util.promisify-将stdout输出为obj或数组而不是换行 - Node.js util.promisify - Output stdout as obj or array instead of new lines Postgres:何时在查询中使用.end() vs.release()(这在 util.promisify 中是否发生了变化)? 缺少这些功能是否会增加 memory 的使用量? - Postgres: When to use .end() vs .release() in queries (and does this change in util.promisify)? Does lack of these functions increase memory usage? _util.default.promisify不是使用节点9.5的函数 - _util.default.promisify is not a function using Node 9.5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM