{"record":{"id":"9b6b57fc1c8c804d","repo":"karatelabs/karate","slug":"sysenv-needs-the-environment-variable-name","errorCode":null,"errorMessage":"sysenv() needs the environment-variable name","messagePattern":"sysenv\\(\\) needs the environment-variable name","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJs.java","lineNumber":463,"sourceCode":"        };\n    }\n\n    /**\n     * Read a file as raw bytes. Useful for binary content handling.\n     * Usage: karate.readAsBytes('path/to/file')\n     */\n    /**\n     * {@code karate.sysenv('NAME')} / {@code karate.sysenv('NAME', 'default')} —\n     * read an OS environment variable. Returns the value as a String. When the\n     * variable is unset or empty, returns the optional second argument (or\n     * {@code null} when no default is supplied) — matches shell {@code ${VAR:-default}}\n     * semantics so the previous {@code karate.sysenv('FOO') || 'default'} idiom\n     * collapses to a single call.\n     */\n    private JavaInvokable sysenv() {\n        return args -> {\n            if (args.length == 0) {\n                throw new RuntimeException(\"sysenv() needs the environment-variable name\");\n            }\n            Object fallback = args.length > 1 ? args[1] : null;\n            Object first = args[0];\n            if (first == null) return fallback;\n            String value = System.getenv(first.toString());\n            return (value == null || value.isEmpty()) ? fallback : value;\n        };\n    }\n\n    /**\n     * {@code karate.sysprop('NAME')} / {@code karate.sysprop('NAME', 'default')} —\n     * read a JVM system property. Cleaner alternative to\n     * {@code karate.properties['NAME']} with first-class default support; reads\n     * from the same merged map (CLI {@code -D}, Maven/Gradle, and {@code Runner.Builder.systemProperties}).\n     */\n    private JavaInvokable sysprop() {\n        return args -> {\n            if (args.length == 0) {","sourceCodeStart":445,"sourceCodeEnd":481,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJs.java#L445-L481","documentation":"karate.sysenv() reads an OS environment variable and supports an optional fallback; the environment-variable name is mandatory. Karate throws this when called with zero arguments because there is no name to look up in System.getenv().","triggerScenarios":"Calling karate.sysenv() with no arguments; forgetting the name when using the fallback form karate.sysenv('FOO', 'default') so only the fallback remains.","commonSituations":"Configuring environment-driven values in karate-config.js and accidentally omitting the variable name; refactoring that removed the first argument but kept the default.","solutions":["Pass the variable name: karate.sysenv('MY_ENV_VAR').","Use the fallback form karate.sysenv('MY_ENV_VAR', 'default') to avoid null/empty results.","Check karate-config.js for calls where the name argument was lost."],"exampleFix":"// before\nvar token = karate.sysenv();\n// after\nvar token = karate.sysenv('API_TOKEN', 'dev-default');","handlingStrategy":"validation","validationCode":"if (!envName) throw new Error('sysenv requires a variable name');\nvar v = karate.sysenv(envName, fallback);","typeGuard":"function validSysenvCall(name, fb) { return typeof name === 'string' && name.length > 0; }","tryCatchPattern":"var v;\ntry { v = karate.sysenv(name, 'default'); }\ncatch (e) { if ((e.message || '').indexOf('sysenv() needs') >= 0) v = 'default'; else throw e; }","preventionTips":["Always name the env var explicitly; use the built-in second-argument fallback instead of omitting the name.","Review karate-config.js refactors for dropped arguments.","Keep a list of required env vars and assert them at config load time."],"tags":["javascript","environment-variables","argument-validation"],"backgroundTag":"missing-required-argument","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"}