{"record":{"id":"9281892f6519ed14","repo":"karatelabs/karate","slug":"key-is-not-defined","errorCode":null,"errorMessage":"${key} is not defined","messagePattern":"(.+?) is not defined","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/CoreContext.java","lineNumber":564,"sourceCode":"        update(key, value, null);\n    }\n\n    void update(String key, Object value, Node node) {\n        if (frameTable != null) {\n            int idx = frameTable.indexOf(key);\n            if (idx >= 0 && frame[idx] != SlotTable.UNDECLARED) {\n                updateSlot(idx, value, node);\n                return;\n            }\n            // undeclared: fall through — a pre-declaration write resolves an\n            // outer/global binding (or creates an implicit global), as today\n        }\n        BindingSlot s = resolve(key);\n        if (s == null) {\n            if (strict) {\n                // Strict mode forbids the sloppy implicit-global creation:\n                // assigning to an unresolvable name is a ReferenceError.\n                throw JsErrorException.referenceError(key + \" is not defined\");\n            }\n            assignImplicitGlobal(key, value, node);\n            return;\n        }\n        if (s.scope == BindScope.CONST && s.initialized) {\n            throw JsErrorException.typeError(\"assignment to constant: \" + key);\n        }\n        // NamedEvaluation (§13.15.2): when RHS is an anonymous function expression and\n        // LHS is an IdentifierRef, set fn.name from the identifier. Mirrors the\n        // declare-path hook above. Skipped for already-named functions so e.g.\n        // `g = someNamedFn` does not clobber the original name.\n        if (value instanceof JsFunction fn && (fn.name == null || fn.name.isEmpty())) {\n            fn.name = key;\n        }\n        Object oldValue = s.value;\n        s.initialized = true;\n        // Unified write — works whether the Slot lives in this context's\n        // bindings, in capturedBindings, in an outer context, or in root.","sourceCodeStart":546,"sourceCodeEnd":582,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/CoreContext.java#L546-L582","documentation":"In strict mode, assigning to a name that cannot be resolved to any binding is a ReferenceError — sloppy-mode implicit global creation is disabled. CoreContext.update throws `key + ' is not defined'` before it would call assignImplicitGlobal, because `strict` is set on the context.","triggerScenarios":"update(key, value) resolving to null with strict=true — e.g. `undeclaredVar = 5` in strict-mode code, or a misspelled variable on the left side of an assignment / compound assignment.","commonSituations":"Typos in assignment targets; code written for sloppy mode run under strict mode ('use strict' or strict defaults); relying on accidental implicit globals across scripts that no longer exist.","solutions":["Declare the variable first (let/var/const) before assigning.","Fix the spelling of the identifier on the assignment's left side.","Remove 'use strict' only as a last resort — prefer explicit declaration.","Check that the variable isn't scoped to another script/evaluation that has ended."],"exampleFix":"// before (strict mode)\nmode = \"fast\";\n// after\nlet mode = \"fast\";","handlingStrategy":"validation","validationCode":"if (typeof targetVar === 'undefined') { throw new ReferenceError('declare targetVar before assigning'); }","typeGuard":"function isDeclared(name) { try { eval(name); return true; } catch (e) { return false; } }","tryCatchPattern":"try { engine.evalRaw(assignScript); } catch (JsErrorException e) { if (e.getMessage().endsWith(\"is not defined\")) { /* declare the variable or fix the typo */ } else throw e; }","preventionTips":["Always declare variables before assigning.","Watch for typos on assignment left-hand sides.","Assume strict mode: never depend on implicit global creation."],"tags":["javascript","strict-mode","reference-error"],"backgroundTag":"reference-not-defined","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}