apify/crawlee · error · Error

The "value" parameter was stringified to JSON and returned u

Error message

The "value" parameter was stringified to JSON and returned undefined. Make sure you're not trying to stringify an undefined value.

What it means

If JSON.stringify returns undefined (which happens for top-level undefined, functions, or symbols), there is nothing storable, so the library throws this explicit error rather than writing an empty/meaningless record. It points out that the caller tried to stringify an undefined value.

Source

Thrown at packages/core/src/storages/key_value_store_codec.ts:63

    if (typeof value === 'string') {
        return { value, contentType: 'text/plain; charset=utf-8' };
    }

    let serialized: string;
    try {
        // Format JSON to simplify debugging, the overheads with compression is negligible
        serialized = jsonStringifyExtended(value as Dictionary, null, 2);
    } catch (e) {
        const error = e as Error;
        // Give more meaningful error message
        if (error.message?.includes('Invalid string length')) {
            error.message = 'Object is too large';
        }
        throw new Error(`The "value" parameter cannot be stringified to JSON: ${error.message}`);
    }

    if (serialized === undefined) {
        throw new Error(
            'The "value" parameter was stringified to JSON and returned undefined. ' +
                "Make sure you're not trying to stringify an undefined value.",
        );
    }

    return { value: serialized, contentType: 'application/json; charset=utf-8' };
}

/**
 * Parses a Buffer or ArrayBuffer using the provided content type header.
 *
 * - application/json is returned as a parsed object.
 * - application/*xml and text/* are returned as strings.
 * - everything else is returned as original body.
 *
 * If the header includes a charset, the body will be stringified only
 * if the charset represents a known encoding to Node.js or Browser.
 *

View on GitHub (pinned to dbe57fb09c)

Solutions

  1. Check that the value is defined before calling setValue (log it or assert)
  2. If the value can legitimately be absent, store null instead of undefined
  3. If you passed a function by mistake, call it and store its result

Example fix

// before
await store.setValue('state', maybeState); // maybeState is undefined
// after
if (maybeState !== undefined) await store.setValue('state', maybeState);
else await store.setValue('state', null);
Defensive patterns

Strategy: validation

Validate before calling

function assertDefined(value, key) {
  if (value === undefined) throw new TypeError(`Refusing to store undefined value for key "${key}"`);
}

Try / catch

try {
  await store.setValue(key, value);
} catch (e) {
  if (e.message.includes('returned undefined')) {
    await store.setValue(key, null); // explicit fallback
  } else throw e;
}

Prevention

When it happens

Trigger: store.setValue('key', undefined); passing a function or symbol directly as value; calling a getter/helper that returns undefined (e.g. a missing variable or a misconfigured optional property like someObject.maybeField).

Common situations: Conditional state that was never assigned; destructuring typos producing undefined; refactors where a function reference was passed instead of its result (setValue('k', getData) instead of setValue('k', getData())).

Related errors


AI-assisted analysis of apify/crawlee@dbe57fb09c (2026-08-30). Data as JSON: /api/errors/b8c42be44f3c9565. Report an issue: GitHub.