karatelabs/karate · error · JsErrorException

Cannot assign to read only property

Error message

Cannot assign to read only property '${name}'

What it means

Thrown when script code writes to a read-only data slot (a property/binding backed by DataSlot whose writable flag is false) while the engine is in strict mode. In non-strict mode the write is silently ignored, matching ECMAScript sloppy-mode behavior. This guards immutable bindings exposed by the host (e.g. built-ins or karate-provided variables that must not be overwritten).

Solutions

  1. Stop assigning to the built-in — choose a different variable name for your own value
  2. If it is your own exposed binding, mark it writable when creating the DataSlot
  3. Remove the assignment; if you need a modified copy, clone into a new variable
  4. Check whether the script is unintentionally strict (module/type=module contexts) if the write was meant to be silently ignored

Example fix

// before
karate = { myHelper: true }; // read-only built-in
// after
var myKarateExt = { myHelper: true };
Defensive patterns

Strategy: type-guard

Validate before calling

// never assign to engine-provided built-ins; pick your own names
var myData = { foo: 1 }; // instead of karate = {...}

Type guard

function isWritableTarget(t) { return t !== karate && !isBuiltInName(t); }

Prevention

When it happens

Trigger: Assigning to a host-provided read-only variable/property such as a karate built-in (`karate = ...`) or a frozen/exposed constant binding, from strict-mode script code; also via any property-write path that resolves to a DataSlot with isWritable() == false.

Common situations: Trying to override karate's built-in `karate` object or engine-provided globals in a feature/script; attempting to redefine library-exposed constants; strict-mode modules copied from browser code that assumed writable globals.

Understand the failure class

Background: Permission denied / not authorized / 403 Forbidden: access-control rejections when the caller lacks the required role, grant, or ownership — this error's family across 18 libraries.

Related errors


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

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/js/DataSlot.java:57

        super(name);
        this.value = value;
    }

    @Override
    boolean isAccessor() {
        return false;
    }

    @Override
    Object read(Object receiver, CoreContext ctx) {
        return value;
    }

    @Override
    void write(Object receiver, Object newValue, CoreContext ctx, boolean strict) {
        if (!isWritable()) {
            if (strict) {
                throw JsErrorException.typeError(
                        "Cannot assign to read only property '" + name + "'");
            }
            return;
        }
        this.value = newValue;
    }

    @Override
    public String toString() {
        return name + "=" + value;
    }

}

View on GitHub (pinned to a22eb90246)