karatelabs/karate · error · ParserException

invalid : import.meta

Error message

invalid ${siteName}: import.meta

What it means

`import.meta` is a meta-property with an invalid AssignmentTargetType per the ES spec, so it cannot be assigned to. Because Karate's lexer does not reserve `import`, this shape parses as a plain REF_DOT_EXPR and is explicitly rejected during assignment-target validation.

Solutions

  1. Read from `import.meta` only; never assign to it
  2. Copy the desired meta value into a variable first, then mutate the variable
  3. If you need custom metadata, store it in your own module-level object instead

Example fix

// before
import.meta.url = otherUrl;
// after
const myUrl = import.meta.url;
// mutate myUrl or your own config object instead
Defensive patterns

Strategy: validation

Validate before calling

if (/import\.meta\s*=[^=]/.test(code) || /\[\s*import\.meta\s*\]/.test(code)) {
  throw new Error('import.meta is not assignable');
}

Type guard

function assignsImportMeta(node) {
  return (node.type === 'AssignmentExpression' || node.type === 'UpdateExpression') &&
    JSON.stringify(node.left).includes('import.meta');
}

Try / catch

try {
  karate.eval(code);
} catch (e) {
  if (String(e.message).includes('import.meta')) { /* copy to variable first */ }
  throw e;
}

Prevention

When it happens

Trigger: Code like `import.meta = x`, `[import.meta] = arr`, or destructuring `({url: import.meta} = o)` where `import.meta` appears in a target position.

Common situations: Misreading `import.meta` as a mutable object; trying to monkey-patch meta information; generated code attempting to write to meta.

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


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

Appendix: source

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

                        && n.getFirst().type == NodeType.FN_ARROW_EXPR) {
                    throw new ParserException("invalid " + siteName + ": arrow function");
                }
                // `this` lexes as IDENT (see JsLexer.keywordOrIdent) but per spec
                // the ThisExpression has AssignmentTargetType=invalid.
                if (n.size() >= 1 && n.getFirst().isToken()
                        && n.getFirst().token.type == IDENT
                        && "this".equals(n.getFirst().getText())) {
                    throw new ParserException("invalid " + siteName + ": this");
                }
            }
            case REF_DOT_EXPR, REF_BRACKET_EXPR -> {
                // Plain member access; a `?.` would have been caught earlier by
                // the optional-chain branch above with a more specific message.
                // `import.meta` is a meta-property whose AssignmentTargetType is
                // invalid; `import` is not a reserved word in our lexer so it
                // parses as a normal REF_DOT_EXPR — flag the literal shape here.
                if (n.type == NodeType.REF_DOT_EXPR && isImportMeta(n)) {
                    throw new ParserException("invalid " + siteName + ": import.meta");
                }
            }
            case FN_CALL_EXPR -> {
                // Web-compat carve-out (Annex B B.3.5): allow `f() = 1` in non-strict mode.
                // Logical-assignment operators (||=, &&=, ??=) are ES2021 and the carve-out
                // does NOT apply to them; the caller passes noCallCarveOut=true in that case.
                if (noCallCarveOut) {
                    throw new ParserException("invalid " + siteName + ": call expression");
                }
            }
            default ->
                    throw new ParserException("invalid " + siteName + ": "
                            + n.type.name() + " is not a valid assignment target");
        }
    }

    /**
     * Strip thin {@code EXPR} / {@code EXPR_LIST} single-child wrappers introduced

View on GitHub (pinned to a22eb90246)