Skip to content Skip to sidebar Skip to footer

Nodejs Util.promisify Is Not A Function

I'm trying to promisify a mysql function but when I run it the console shows this error util.Promisify is not a function. This is my code: The same error if var query = util.prom

Solution 1:

util.promisify is a part of Node 8.X version. But you can still have a polyfill for the older version of Node.

A polyfill is available to take care of the older version of node servers you’ll be running your application on. It can be installed via npm in the following manner:

npm install util.promisify

Now you can patch module utl on older versions of Node

const util = require('util');
require('util.promisify').shim();

const fs = require('fs');
const readFileAsync = util.promisify(fs.readFile);

Quoted from https://grizzlybit.info/blog/nodejs-util-promisify

Solution 2:

Unless you're using Node.js 8.x this function won't be defined, that's when it was added to the core Utilities library.

As util is a core Node.js library, you shouldn't have to install it. If you're using Node.js 6.x then use a library like Bluebird which has a promisify function.

Solution 3:

Other people have spoken to the solution, but here is another source of this error:

There's a NPM package es6-promisify that can also give the error message TypeError: promisify is not a function. (That's why I got to this question.)

  • Version 5.0.0 of es6-promisifiy needed const promisify = require("es6-promisify"); then you'd use result = promisify( ... );

  • Version 6.0.0 had a change of API, and the declaration changed to const { promisify } = require("es6-promisify");

Solution 4:

You can promise it by yourself if you want: const promisify = f => (...args) => new Promise((a,b)=>f(...args, (err, res) => err ? b(err) : a(res)));

Solution 5:

Util is included from Node 8.x on so if you can update node I'd do that.

Post a Comment for "Nodejs Util.promisify Is Not A Function"