karatelabs/karate · error · JsErrorException

Function constructor unavailable: no engine

Error message

Function constructor unavailable: no engine

What it means

new Function(...) compiles the body via the engine's evalRaw. When the running Context has no Engine attached (a non-evaluating or restricted context), Karate cannot compile the dynamic function and throws this TypeError. It is a host-environment limitation, not a spec-mandated JS error.

Solutions

  1. Run the code in a context with an Engine available (use the standard Karate JS engine context).
  2. Replace dynamic new Function compilation with a pre-defined function or static expression.
  3. Feature-detect: if engine compilation is unavailable, fall back to an interpreted path or disable the dynamic-function feature.

Example fix

// before
const f = new Function('x', 'return x * 2'); // TypeError: no engine
// after
const f = (x) => x * 2; // no dynamic compilation
Defensive patterns

Strategy: fallback

Validate before calling

if (typeof engine === 'undefined' || engine === null) throw new Error('dynamic Function compilation unavailable in this context');

Type guard

function canCompileDynamic(ctx) { return ctx && typeof ctx.getEngine === 'function' && ctx.getEngine() != null; }

Try / catch

try { return new Function(args, body); } catch (e) { if (e instanceof TypeError && /no engine/.test(e.message)) return staticFallback; throw e; }

Prevention

When it happens

Trigger: Calling new Function('return 1') (or Function('...')) inside a Karate JS context configured without an engine — e.g. embedded/host-embedded contexts, static analysis of expressions, or contexts created purely for introspection.

Common situations: Libraries that lazily compile functions from strings (formatters, expression evaluators) running inside engine-less Karate contexts; sandboxed configurations that strip the compiler.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/89aa99fc743bf695. Report an issue: GitHub.

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/js/JsFunctionConstructor.java:65

    }

    @Override
    public Object call(Context context, Object[] args) {
        StringBuilder src = new StringBuilder("(function anonymous(");
        if (args.length == 0) {
            src.append(") {\n})");
        } else {
            for (int i = 0; i < args.length - 1; i++) {
                if (i > 0) src.append(',');
                src.append(argToString(args[i]));
            }
            src.append("\n) {\n");
            src.append(argToString(args[args.length - 1]));
            src.append("\n})");
        }
        Engine engine = context.getEngine();
        if (engine == null) {
            throw JsErrorException.typeError("Function constructor unavailable: no engine");
        }
        try {
            return engine.evalRaw(src.toString());
        } catch (io.karatelabs.parser.ParserException e) {
            // CreateDynamicFunction (§20.2.1.1): an unparsable body is a JS
            // SyntaxError, not a host parse exception
            throw JsErrorException.syntaxError(e.getMessage());
        }
    }

    private static String argToString(Object arg) {
        if (arg == null || arg == Terms.UNDEFINED) return "";
        return arg.toString();
    }

}

View on GitHub (pinned to a22eb90246)