karatelabs/karate · error · ParserException

' ' is not a valid function name in strict mode

Error message

'${name}' is not a valid function name in strict mode

What it means

In strict mode, `eval` and `arguments` may not be used as function names. Karate's JS parser checks the identifier following a (possibly `async`) `function` keyword and rejects it when the script is parsed as strict. This mirrors the ECMAScript spec's BindingIdentifier restrictions.

Solutions

  1. Rename the function (e.g. `collectArguments`, `evalExpr`).
  2. If the function genuinely wraps `arguments`, access the real `arguments` object inside a normal (non-arrow) function instead of naming a function `arguments`.
  3. Avoid the strict context: declare at top level in sloppy mode if the library permits, but renaming is the correct fix.

Example fix

// before
"use strict";
function eval(expr) { return karate.eval(expr); }
// after
"use strict";
function evalExpr(expr) { return karate.eval(expr); }
Defensive patterns

Strategy: validation

Validate before calling

function assertValidFnName(src) {
  const m = src.match(/(async\s+)?function\s*[&*]?\s*([\w$]+)/);
  if (m && /^(eval|arguments)$/.test(m[2])) {
    throw new Error("'" + m[2] + "' cannot be a function name in strict mode");
  }
}

Try / catch

try {
  karate.eval(script);
} catch (e) {
  if (String(e.message).includes('is not a valid function name in strict mode')) {
    const bad = String(e.message).match(/'(\w+)'/)[1];
    script = script.replace(new RegExp('\\b' + bad + '\\b', 'g'), bad + '_fn');
  } else throw e;
}

Prevention

When it happens

Trigger: Declaring `function eval() {}`, `function arguments() {}`, or their `async` equivalents in strict-mode Karate JS (e.g. scripts where strict mode applies via module/class code or Karate's parsing configuration).

Common situations: Porting old utility code that used `arguments` or `eval` as names; shadowing built-ins out of habit; code that worked in sloppy mode breaking when moved into a strict context (class body, module).

Related errors


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

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/parser/JsParser.java:993

            Node child = fn.get(i);
            if (!child.isToken() && child.type == NodeType.BLOCK) {
                return hasUseStrictPrologue(child);
            }
        }
        return false;
    }

    /** A strict function may not be named {@code eval} / {@code arguments}. The name
     *  is the single IDENT token between {@code function} and the parameter list;
     *  arrows and anonymous expressions reach FN_DECL_ARGS first and return. */
    private static void checkFunctionName(Node fn) {
        // an `async function` puts the contextual `async` IDENT before the keyword
        for (int i = fn.async ? 1 : 0, n = fn.size(); i < n; i++) {
            Node child = fn.get(i);
            if (child.isToken()) {
                if (child.token.type == IDENT) {
                    if (isEvalOrArguments(child.getText())) {
                        throw new ParserException(
                                "'" + child.getText() + "' is not a valid function name in strict mode");
                    }
                    return;
                }
            } else if (child.type == NodeType.FN_DECL_ARGS) {
                return; // reached the parameter list — function was anonymous
            }
        }
    }

    /**
     * Formal-parameter early errors over the full BoundNames of the parameter list:
     * <ul>
     *   <li>Duplicate bound names are a SyntaxError when the list is non-simple (any
     *       destructuring pattern, default, or rest element), when the function is an
     *       arrow (UniqueFormalParameters), or when the function is strict. Plain
     *       duplicate simple params in a sloppy non-arrow function stay legal.</li>
     *   <li>Under strict, no bound name may be {@code eval} / {@code arguments}.</li>

View on GitHub (pinned to a22eb90246)