karatelabs/karate · error · ParserException

duplicate parameter name

Error message

duplicate parameter name '${name}' is not allowed here

What it means

Duplicate parameter names are allowed only for simple, non-strict functions. Karate's parser rejects duplicates whenever the function is strict, is an arrow function, or has any non-simple parameter (destructuring pattern, default value, rest parameter) — matching the ECMAScript rule that such functions must have unique formal parameter names.

Solutions

  1. Rename one of the duplicate parameters and update its uses in the body.
  2. If the last-wins behavior mattered, merge into one parameter or pick defaults: `function f(a, b = a) {}`.
  3. Remove the newly added destructuring/default if it accidentally made a previously tolerated duplicate illegal — but prefer renaming.

Example fix

// before
function configure(opts, opts) { return use(opts); }
// after
function configure(opts, overrides = opts) { return use({ ...opts, ...overrides }); }
Defensive patterns

Strategy: validation

Validate before calling

function assertUniqueParams(fnSrc) {
  const m = fnSrc.match(/\(([^)]*)\)/);
  if (!m) return;
  const names = m[1].split(',').map(s => s.trim()).filter(Boolean);
  const seen = new Set();
  for (const n of names) {
    const key = n.split(/[=:]/)[0].trim();
    if (seen.has(key)) throw new Error('duplicate parameter: ' + key);
    seen.add(key);
  }
}

Try / catch

try {
  karate.eval(script);
} catch (e) {
  if (String(e.message).includes('duplicate parameter name')) {
    // rename the duplicate in the function signature and body
  } else throw e;
}

Prevention

When it happens

Trigger: `function f(a, a) {}` combined with strict mode or any destructuring/default/rest param (e.g. `function f({x}, {x}) {}`, `(a, a) => ...`, `function f(a = 1, a) {}`) in Karate JS.

Common situations: Old-style code relying on duplicate params where the last wins (`function f(opts, opts)`); adding a default value or destructuring to a legacy function with duplicate names, which suddenly makes duplicates illegal.

Understand the failure class

Background: "must be a positive integer", "cannot be empty", "invalid argument": how invalid-argument errors work across open-source libraries — this error's family across 33 libraries.

Related errors


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

Appendix: source

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

        for (int i = 0, n = args.size(); i < n; i++) {
            Node arg = args.get(i);
            if (arg.isToken() || arg.type != NodeType.FN_DECL_ARG) {
                continue;
            }
            collectBindingBoundNames(arg, names);
        }
        if (strict) {
            for (String name : names) {
                if (isEvalOrArguments(name)) {
                    throw new ParserException(
                            "'" + name + "' is not a valid parameter name in strict mode");
                }
            }
        }
        if (strict || isArrow || nonSimple) {
            String dup = firstDuplicate(names);
            if (dup != null) {
                throw new ParserException(
                        "duplicate parameter name '" + dup + "' is not allowed here");
            }
        }
    }

    /** A FormalParameter is non-simple if it is a destructuring pattern, carries a
     *  default initializer, or is a rest element — any of which makes the whole
     *  parameter list non-simple, so duplicate bound names become an early error. */
    private static boolean isNonSimpleParam(Node arg) {
        for (int i = 0, n = arg.size(); i < n; i++) {
            Node ch = arg.get(i);
            if (ch.isToken()) {
                if (ch.token.type == EQ || ch.token.type == DOT_DOT_DOT) {
                    return true;
                }
            } else if (ch.type == NodeType.LIT_ARRAY || ch.type == NodeType.LIT_OBJECT) {
                return true;
            }

View on GitHub (pinned to a22eb90246)