petkaantonov/bluebird · error · TypeError

the target of promisifyAll must be an object or a function

Error message

the target of promisifyAll must be an object or a function

    See http://goo.gl/MqrFmX

What it means

`Promise.promisifyAll(target)` requires the target to be an object or a function, since it enumerates and copies the target's properties to build suffixed promisified versions. Any other type (string, number, boolean, null, undefined) throws this TypeError.

Source

Thrown at src/promisify.js:297

Promise.promisify = function (fn, options) {
    if (typeof fn !== "function") {
        throw new TypeError(FUNCTION_ERROR + util.classString(fn));
    }
    if (isPromisified(fn)) {
        return fn;
    }
    options = Object(options);
    var receiver = options.context === undefined ? THIS : options.context;
    var multiArgs = !!options.multiArgs;
    var ret = promisify(fn, receiver, multiArgs);
    util.copyDescriptors(fn, ret, propsFilter);
    return ret;
};

Promise.promisifyAll = function (target, options) {
    if (typeof target !== "function" && typeof target !== "object") {
        throw new TypeError(PROMISIFY_TYPE_ERROR);
    }
    options = Object(options);
    var multiArgs = !!options.multiArgs;
    var suffix = options.suffix;
    if (typeof suffix !== "string") suffix = defaultSuffix;
    var filter = options.filter;
    if (typeof filter !== "function") filter = defaultFilter;
    var promisifier = options.promisifier;
    if (typeof promisifier !== "function") promisifier = makeNodePromisified;

    if (!util.isIdentifier(suffix)) {
        throw new RangeError(SUFFIX_NOT_IDENTIFIER);
    }

    var keys = util.inheritedDataKeys(target);
    for (var i = 0; i < keys.length; ++i) {
        var value = target[keys[i]];
        if (keys[i] !== "constructor" &&

View on GitHub (pinned to c220cfe480)

Solutions

  1. Pass the module object: `Promise.promisifyAll(require('fs'))`.
  2. Check the require/import resolves to a defined object or function before calling.
  3. For ESM interop, use `mod.default ?? mod` before promisifyAll.

Example fix

// before
const fs = require('fs'); const pfs = Promise.promisifyAll(fs.path); // undefined
// after
const pfs = Promise.promisifyAll(fs);
Defensive patterns

Strategy: validation

Validate before calling

const mod = require('fs');
if (mod == null || (typeof mod !== 'object' && typeof mod !== 'function')) {
  throw new TypeError('promisifyAll target must be an object or function');
}
Promise.promisifyAll(mod);

Type guard

const isPromisifyAllTarget = (v) => v != null && (typeof v === 'object' || typeof v === 'function');

Try / catch

try { Promise.promisifyAll(target); } catch (e) {
  if (e instanceof TypeError && String(e).includes('promisifyAll')) {
    throw new Error('Bad promisifyAll target: ' + String(target));
  }
  throw e;
}

Prevention

When it happens

Trigger: `Promise.promisifyAll(null)`; passing a module namespace that failed to load (undefined); passing a primitive like `Promise.promisifyAll('fs')`; requiring a CommonJS module with `import` so you get the module wrapper's default mismatch.

Common situations: Failed/bad require producing undefined at startup; ESM/CJS interop giving a string or unexpected default export; typo'd variable holding a primitive.

Related errors


AI-assisted analysis of petkaantonov/bluebird@c220cfe480 (2026-09-02). Data as JSON: /api/errors/728d3933485a6958. Report an issue: GitHub.