karatelabs/karate · error · JsErrorException

Array.from requires an iterable or array-like object, not

Error message

Array.from requires an iterable or array-like object, not 

What it means

Array.from requires its first argument to be an iterable (has @@iterator) or an array-like object (has a usable length). Passing null, undefined, or no argument at all throws this TypeError. Note the message concatenates the offending value, so calling with zero args reads 'not undefined'.

Solutions

  1. Check the argument is non-null/non-undefined before calling Array.from
  2. Convert plain objects with Object.values/Object.entries or Array.of for literal values
  3. Provide a default (Array.from(x || [])) when the source may be absent

Example fix

// before
Array.from(response.items); // items undefined -> TypeError
// after
Array.from(response.items || []);
Defensive patterns

Strategy: type-guard

Validate before calling

function safeArrayFrom(x) { return x == null ? [] : Array.from(x); }

Type guard

function isIterableOrArrayLike(x) { return x != null && (typeof x[Symbol.iterator] === 'function' || (typeof x.length === 'number' && x.length >= 0)); }

Try / catch

try { var a = Array.from(src); } catch (e) { if (String(e).indexOf('Array.from requires') !== -1) { a = []; } else { throw e; } }

Prevention

When it happens

Trigger: Array.from() with no args; Array.from(null); Array.from(undefined); Array.from on a value that is neither iterable nor array-like reaches this same throw when args[0] is null/undefined.

Common situations: Passing a possibly-null variable (e.g. a missing API response) straight into Array.from; forgetting that plain objects without length/iterator are not array-like; query results that came back undefined.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/js/JsArrayConstructor.java:66

    private void installIntrinsics() {
        defineOwn("from", new JsBuiltinMethod("from", 1, this::from), METHOD_ATTRS);
        defineOwn("isArray", new JsBuiltinMethod("isArray", 1, (JsInvokable) this::isArray), METHOD_ATTRS);
        defineOwn("of", new JsBuiltinMethod("of", 0, (JsInvokable) this::of), METHOD_ATTRS);
        defineOwn("prototype", JsArrayPrototype.INSTANCE, PropertySlot.INTRINSIC);
    }

    @Override
    public Object call(Context context, Object[] args) {
        return JsArray.create(args);
    }

    // Static methods

    @SuppressWarnings("unchecked")
    private Object from(Context context, Object[] args) {
        if (args.length == 0 || args[0] == null || args[0] == Terms.UNDEFINED) {
            throw JsErrorException.typeError("Array.from requires an iterable or array-like object, not " + (args.length == 0 ? "undefined" : args[0]));
        }
        Object source = args[0];
        JsCallable mapFn = (args.length > 1 && args[1] instanceof JsCallable) ? (JsCallable) args[1] : null;
        List<Object> results = new ArrayList<>();
        // Iterable path: anything with @@iterator (built-in or user). Per spec, this
        // takes priority over the array-like length-walk fallback.
        JsIterator iter = IterUtils.tryGetIterator(source, context);
        if (iter != null) {
            int index = 0;
            while (iter.hasNext()) {
                Object v = iter.next();
                Object mapped = mapFn == null ? v : mapFn.call(context, new Object[]{v, index});
                results.add(mapped);
                index++;
            }
            // Spec §23.1.2.1 returns an Array exotic — wrap so
            // `Array.from(...).constructor === Array` and
            // `Array.from(...) instanceof Array` hold.

View on GitHub (pinned to a22eb90246)