karatelabs/karate · error · RuntimeException

read() needs at least one argument

Error message

read() needs at least one argument

What it means

karate.read() requires at least one argument: the path of the file (feature, json, xml, etc.) to load. Karate throws this immediately when the JS engine invokes the `read` callable with an empty argument list, since there is no meaningful default file to resolve. It is an argument-validation guard at the top of initRead().

Solutions

  1. Pass the file path as the first argument, e.g. karate.read('classpath:my.feature').
  2. Verify the variable holding the path is defined and non-empty before the call.
  3. If using apply/spread, ensure the arguments array has at least one element.
  4. Check interpolation syntax so the path expression does not silently evaluate to nothing.

Example fix

// before
var text = karate.read();
// after
var text = karate.read('classpath:data/payload.json');
Defensive patterns

Strategy: validation

Validate before calling

if (!path) throw new Error('read() requires a file path');
var text = karate.read('classpath:' + path);

Type guard

function hasReadArg(args) { return Array.isArray(args) && args.length > 0 && args[0] != null && args[0] !== ''; }

Try / catch

var text;
try { text = karate.read(path); }
catch (e) { if ((e.message || '').indexOf('read() needs at least one argument') >= 0) karate.fail('read() called without a path'); throw e; }

Prevention

When it happens

Trigger: Calling karate.read() with zero arguments, e.g. read() or read(...apply() with an empty args array) in a JS block or karate.call expression where the path argument evaluates away.

Common situations: Interpolating the path from a variable that is undefined/empty so the call collapses to no argument; typos like karate.read; dynamically building read calls in JS where an array spread passes nothing.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/core/KarateJs.java:203

            Map<String, Object> vars;
            if (args.length > 1) {
                vars = (Map<String, Object>) args[1];
            } else {
                vars = null;
            }
            String html = markup().processPath(read, vars);
            onDoc.accept(html);
            return null;
        };
    }

    // ========== Engine-Dependent Methods ==========
    // These methods require access to the JavaScript engine for evaluation.

    private JavaCallable initRead() {
        return (context, args) -> {
            if (args.length == 0) {
                throw new RuntimeException("read() needs at least one argument");
            }
            String rawPath = args[0] + "";

            // Parse tag selector for feature files
            // Supports: file.feature@tag or @tag (same-file)
            String path;
            String tagSelector = null;
            if (rawPath.startsWith("@")) {
                // Same-file tag - return a FeatureCall wrapper
                return new FeatureCall(null, rawPath);
            } else {
                int tagPos = rawPath.indexOf(".feature@");
                if (tagPos != -1) {
                    path = rawPath.substring(0, tagPos + 8);  // "file.feature"
                    tagSelector = "@" + rawPath.substring(tagPos + 9);  // "@tag"
                } else {
                    path = rawPath;
                }

View on GitHub (pinned to a22eb90246)