karatelabs/karate · error · ParserException
invalid assignment to
Error message
invalid assignment to '${name}' in strict mode What it means
Strict-mode early error: an assignment targets `eval` or `arguments` as a simple identifier, which the ECMAScript spec forbids in strict mode (and Karate's parser enforces). These names lose their special meaning in strict mode and may not be assigned, declared, or used as parameter names. Parsing fails before the script runs.
Solutions
- Rename the variable (e.g. `evalFn`, `args`) — `eval`/`arguments` are reserved as assignment targets in strict mode
- Remove the assignment if it was unintentional (e.g. trying to overwrite built-in eval)
- If assignment is truly needed, restructure to return a value instead of mutating `eval`/`arguments`
Example fix
// before eval = myCode; // strict-mode violation // after var evalFn = myCode; result = evalFn();
Defensive patterns
Strategy: validation
Validate before calling
// reject assignments to eval/arguments before parsing
if (/\b(?:eval|arguments)\s*=[^=]/.test(src)) {
throw new Error('script assigns to eval/arguments: not allowed in strict mode');
} Try / catch
try {
karate.runJs(src);
} catch (e) {
if (String(e).includes("invalid assignment to")) {
// rename eval/arguments variable in the script
}
throw e;
} Prevention
- Never name variables `eval` or `arguments`
- Enable ESLint no-shadow-restricted-names in the project
- Review ported legacy scripts for reserved-name usage
When it happens
Trigger: Parsing code like `eval = 5;` or `arguments = something;` in strict mode — checkStrictInvalidAssignment detects an IDENT reference that isEvalOrArguments matches on the left-hand side of an assignment.
Common situations: Porting old sloppy-mode scripts that used `eval` or `arguments` as ordinary variable names; shadowing bugs where a helper was named `eval`; minified/generated code assigning to these reserved names.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- ' ' is not a valid function name in strict mode
- duplicate parameter name
- identifier ' ' has already been declared
- octal literals are not allowed in strict mode
- invalid : call expression
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/8f837d424713b74d.
Report an issue: GitHub.
Appendix: source
Thrown at karate-js/src/main/java/io/karatelabs/parser/JsParser.java:1473
if (!seen.add(name)) {
return name;
}
}
return null;
}
/** A strict assignment / update may not target {@code eval} / {@code arguments}.
* Only a bare identifier reference is an early error; member targets
* ({@code arguments[0] = …}) are fine. */
private void checkAssignTargetBinding(Node lhs) {
if (lhs == null) {
return;
}
Node n = stripExprWrappers(lhs);
if (n != null && n.type == NodeType.REF_EXPR && n.size() == 1
&& n.getFirst().isToken() && n.getFirst().token.type == IDENT
&& isEvalOrArguments(n.getFirst().getText())) {
throw new ParserException(
"invalid assignment to '" + n.getFirst().getText() + "' in strict mode");
}
}
/** Strict-mode early error for a legacy octal ({@code 0755}) or NonOctalDecimal
* ({@code 08} / {@code 09}) integer literal — any NUMBER whose text starts with
* {@code 0} immediately followed by a decimal digit. {@code 0x…} / {@code 0b…} /
* {@code 0o…} / {@code 0.…} / {@code 0e…} have a non-digit second char and a plain
* {@code 0} is length 1, so all are correctly excluded. */
private static void checkLegacyOctalLiteral(Node litExpr) {
if (litExpr.size() != 1) {
return;
}
Node tok = litExpr.getFirst();
if (!tok.isToken() || tok.token.type != NUMBER) {
return;
}
String text = tok.getText();View on GitHub (pinned to a22eb90246)