Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

TypeError: Right-hand side of 'instanceof' is not callable

ErrorTypeError: Right-hand side of 'instanceof' is not callable

Short answer

instanceof needs a function (a class) on its right side. Imported from another module, the name often holds an object instead: the whole exports object, an unfinished copy from a circular require, or an ES module namespace. Print typeof on that name right before the check, then fix the export or the import.

  • Applies to Node.js 22, Node.js 24, CommonJS, ES modules
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print what the right-hand side really is, on the line just before the failing check. A class prints 'function'; anything else tells you which cause below applies.

    console.log(typeof Foo, Foo);
    if (value instanceof Foo) { /* ... */ }
  2. 2Match what you see, then jump to its fix

    1. 1typeof Foo is 'object', and the object has a property with the class inside, such as { Foo: [class Foo] }.The module exports an object, but you use it as the classFix
    2. 2typeof Foo is 'object' and the object is empty (or lacks Foo), but the export looks correct when you read foo.js.A circular require handed you an unfinished exports objectFix
    3. 3The file uses import * as Foo from '...', and the check says value instanceof Foo.An ES module namespace import is used as the classFix

What does the error look like?

  • TypeError: Right-hand side of 'instanceof' is not callable
  • TypeError: invalid 'instanceof' operand 'x'

Cause 1 of 3

The module exports an object, but you use it as the class

1Check

Look at how the other file exports. module.exports = { Foo } and exports.Foo = Foo both export an object that holds the class. Reassigning exports itself exports nothing: exports = Foo only changes a local variable and the importer gets an empty object.

grep -n "module.exports\|exports\." src/foo.js
grep -n "require(" src/consumer.js

2Fix

Match the two sides. Either destructure the class where you import it, or export the class itself with module.exports.

// foo.js, option A: export an object, import with destructuring
module.exports = { Foo };
// consumer.js
const { Foo } = require('./foo');

// foo.js, option B: export the class itself
module.exports = Foo;
// consumer.js
const Foo = require('./foo');

3Verify

Run the same check again. It prints 'function', and the instanceof line runs.

node -e "const { Foo } = require('./src/foo'); console.log(typeof Foo)"

Confirmed by nodejs.org

Did this fix it?

Cause 2 of 3

A circular require handed you an unfinished exports object

1Check

With circular require() calls, Node.js returns an unfinished copy of the other module's exports to the module that asked first. If foo.js requires bar.js and bar.js requires foo.js, loading foo.js first makes bar.js receive foo's unfinished exports, which can still be an empty object because the class has not been exported yet.

// add temporarily at the top of src/bar.js
console.log('bar sees foo as', require('./foo'));

2Fix

Break the cycle. Move the code both files need into a third module, or require the other file inside the function that uses it, after both modules have finished loading.

// bar.js: require at call time, not at load time
function isFoo(value) {
  const { Foo } = require('./foo');
  return value instanceof Foo;
}
module.exports = { isFoo };

3Verify

Run the app and call isFoo with a Foo instance and with another value.

node -e "const { isFoo } = require('./src/bar'); const { Foo } = require('./src/foo'); console.log(isFoo(new Foo()), isFoo({}))"

Confirmed by nodejs.org

Did this fix it?

Cause 3 of 3

An ES module namespace import is used as the class

1Check

import * as name creates a namespace object that holds all exports as properties, and the default export sits under the key default. An object like that is not a function, so instanceof throws.

grep -rn "import \* as" src/

2Fix

Import the class by name or as the default export, whichever the module provides.

// named export
import { Foo } from './foo.js';

// default export
import Foo from './foo.js';

3Verify

Run the file again. instanceof now compares against the class.

node src/consumer.js

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Export one class per file in one consistent style (named exports, or module.exports of the class), and import it in the matching style.
  • Keep dependencies one-way: if two files need each other, extract what they share into a third file.
  • Use type checking (TypeScript, or JSDoc types in plain JavaScript), so an object used where a class is required is flagged in the editor.
  • Avoid 'import * as' for a module whose exports you use as a single class.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.