karatelabs/karate · error · RuntimeException

write() needs value and path arguments

Error message

write() needs value and path arguments

What it means

karate.write(value, path) writes content to a file under the output directory (e.g. target/karate-reports). It requires exactly two arguments — the value to write and the target path — and throws when fewer than two are supplied.

Solutions

  1. Supply both arguments: karate.write(content, 'output/result.txt')
  2. Ensure the path variable is defined and a string before the call
  3. Remember the path is relative to the output dir, not the classpath — pass a simple relative filename.

Example fix

// before
karate.write(JSON.stringify(payload));
// after
karate.write(JSON.stringify(payload), 'payload.json');
Defensive patterns

Strategy: validation

Validate before calling

// JS
if (value == null || path == null) karate.fail('karate.write needs value and path');
karate.write(value, path);

Type guard

function canWrite(v, p) { return arguments.length === 2 && p != null; }

Try / catch

try { karate.write(value, path); } catch (e) { karate.warn('write failed: ' + e); }

Prevention

When it happens

Trigger: `karate.write(value)` missing the path, `karate.write()` with nothing, or `karate.write(null)` — all have args.length < 2.

Common situations: Forgetting the path because a default was assumed; an optional path variable that was undefined leaving only one effective argument; translating karate.write(filePath, value) with swapped/merged arguments from older API memory.

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/33b16537e91c01cb. Report an issue: GitHub.

Appendix: source

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

            }
            String path = args[0] + "";
            Resource resource = getCurrentResource().resolve(path);
            if (resource.isFile() && resource.getPath() != null) {
                return resource.getPath().toAbsolutePath().toString();
            }
            // For classpath resources, return the prefixed path
            return resource.getPrefixedPath();
        };
    }

    /**
     * karate.write(value, path) - Write content to a file.
     * Path is relative to the output directory (e.g., target/karate-reports).
     */
    private JavaInvokable write() {
        return args -> {
            if (args.length < 2) {
                throw new RuntimeException("write() needs value and path arguments");
            }
            Object value = args[0];
            String path = args[1] + "";

            // Get output directory
            String outputDir = getOutputDir();

            // Create the full path
            File file = new File(outputDir, path);

            // Ensure parent directories exist
            File parent = file.getParentFile();
            if (parent != null && !parent.exists()) {
                parent.mkdirs();
            }

            // Convert value to bytes
            byte[] bytes = KarateJsUtils.convertToBytes(value);

View on GitHub (pinned to a22eb90246)