{"record":{"id":"b6ff9c774c0acf9c","repo":"karatelabs/karate","slug":"target-is-not-a-constructor","errorCode":null,"errorMessage":"target is not a constructor","messagePattern":"target is not a constructor","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/Interpreter.java","lineNumber":786,"sourceCode":"        Object callable = evalFnTaggedTemplate(operand, context);\n        if (context.isError()) {\n            return Terms.UNDEFINED;\n        }\n        if (!(callable instanceof JsCallable c) || !c.isConstructable()) {\n            throw JsErrorException.typeError(operand.getTextIncludingWhitespace() + \" is not a constructor\");\n        }\n        return invokeAsConstructor(c, new Object[0], operand, context);\n    }\n\n    /**\n     * Construct {@code callable} as if invoked via {@code new}. Used by\n     * {@code Reflect.construct} which has no syntactic Node to thread through;\n     * the engine creates a synthetic placeholder so event tracing has something\n     * to attach to. Throws TypeError if the target is not constructable.\n     */\n    static Object constructFromHost(JsCallable callable, Object[] args, CoreContext context) {\n        if (!callable.isConstructable()) {\n            throw JsErrorException.typeError(\"target is not a constructor\");\n        }\n        return invokeAsConstructor(callable, args, new Node(NodeType.NEW_EXPR), context);\n    }\n\n    private static Object invokeAsConstructor(JsCallable callable, Object[] args, Node node, CoreContext context) {\n        CoreContext callContext;\n        ObjectLike newInstance = null;\n        Object result;\n        if (callable instanceof JsFunctionNode jsFunc) {\n            callContext = new CoreContext(context, node, args, jsFunc.declaredContext, jsFunc.capturedBindings);\n            callContext.strict = jsFunc.strict;\n            callContext.callInfo = new CallInfo(true, callable);\n            callContext.privateEnv = jsFunc.privateEnv;\n            callContext.activeFunction = jsFunc;\n            newInstance = allocateInstance(jsFunc);\n            Object proto = jsFunc.getMember(\"prototype\");\n            if (proto instanceof ObjectLike protoObj) {\n                setInstancePrototype(newInstance, protoObj);","sourceCodeStart":768,"sourceCodeEnd":804,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/Interpreter.java#L768-L804","documentation":"Thrown by `Reflect.construct` support when the target callable lacks a [[Construct]] slot. `constructFromHost` is the host-side path for Reflect.construct with no syntactic `new` node, so the message names the 'target' argument per the spec. This mirrors V8's `Reflect.construct: target is not a constructor` TypeError.","triggerScenarios":"Calling `Reflect.construct(target, args)` (or host code invoking Interpreter.constructFromHost) where target is an arrow function, a method, a bound non-constructable callable, or a plain object.","commonSituations":"Utility code that dynamically instantiates classes via Reflect.construct receiving user-supplied or resolved-at-runtime values that turn out not to be classes.","solutions":["Check `Reflect.construct`'s first argument is a class or regular function before calling it.","Guard with a typeof/isFunction check and fall back to a normal call if not constructable.","Fix resolution logic so the intended class (not an arrow function or import default) is passed."],"exampleFix":"// before\nvar o = Reflect.construct(makeThing, []);\n// after\nvar Thing = resolveClass();\nif (typeof Thing !== 'function') throw new Error('expected a class');\nvar o = Reflect.construct(Thing, []);","handlingStrategy":"type-guard","validationCode":"if (typeof target !== 'function') throw new Error('Reflect.construct target must be a constructor');","typeGuard":"function isConstructableTarget(v) { return typeof v === 'function' && !/=>/.test(String(v)); }","tryCatchPattern":"try { return Reflect.construct(target, args); } catch (e) { if (String(e).includes('not a constructor')) return null; throw e; }","preventionTips":["Validate Reflect.construct targets are classes","Avoid mutating class bindings passed to Reflect.construct","Keep a single source of truth for constructor references"],"tags":["javascript","reflect-construct","interpreter"],"backgroundTag":"not-a-constructor","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}