Documents injecute 1.x · Migrating from 0.x
Skip to content

INJECUTE_CLASS_NOT_CONSTRUCTED ​

A class was called without new.

How to fix it ​

Classes compiled to ES5 (Babel for old browsers, TypeScript target: es5) and bound classes cannot be detected. Register them with construct(MyClass).

Why it happens ​

Native classes are detected and constructed with new. Two kinds of classes look like plain functions:

  • classes compiled to ES5 (Babel targeting old browsers, TypeScript target: es5, prebuilt ES5 libraries in node_modules);
  • bound classes (MyClass.bind(null)).

The container calls them without new, and the engine throws, for example "Class constructor X cannot be invoked without 'new'" (V8), "Cannot call a class constructor without |new|" (JavaScriptCore) or "Cannot call a class as a function" (Babel).

Fix ​

ts
import { construct } from 'injecute';

app.addSingleton('client', construct(LegacyClient), ['config']);

An ES5 constructor that has no prototype methods and never uses this cannot be detected at all: it resolves to undefined. Use construct() for it too.

Handling it in code ​

ts
import { InjecuteError } from 'injecute';

try {
  // ...
} catch (error) {
  if (error instanceof InjecuteError && error.code === 'INJECUTE_CLASS_NOT_CONSTRUCTED') {
    // error.message explains what happened; error.path is the resolution chain
  }
}

All error codes

Released under the MIT License.