sveltejs/svelte · error · Error

derived() expects stores as input, got a falsy value

Error message

derived() expects stores as input, got a falsy value

What it means

`derived(stores, fn)` synthesizes one store from others. It validates that every store in the array (or the single store) is truthy — not `null`/`undefined`/`0`/`''`/`false`. A falsy entry usually means a store was never initialized or an import failed. The guard runs before subscribing, so it fails fast.

Source

Thrown at packages/svelte/src/store/shared/index.js:134

 * @param {S} stores
 * @param {(values: StoresValues<S>) => T} fn
 * @param {T} [initial_value]
 * @returns {Readable<T>}
 */
/**
 * @template {Stores} S
 * @template T
 * @param {S} stores
 * @param {Function} fn
 * @param {T} [initial_value]
 * @returns {Readable<T>}
 */
export function derived(stores, fn, initial_value) {
	const single = !Array.isArray(stores);
	/** @type {Array<Readable<any>>} */
	const stores_array = single ? [stores] : stores;
	if (!stores_array.every(Boolean)) {
		throw new Error('derived() expects stores as input, got a falsy value');
	}
	const auto = fn.length < 2;
	return readable(initial_value, (set, update) => {
		let started = false;
		/** @type {T[]} */
		const values = [];
		let pending = 0;
		let cleanup = noop;
		const sync = () => {
			if (pending) {
				return;
			}
			cleanup();
			const result = fn(single ? values[0] : values, set, update);
			if (auto) {
				set(result);
			} else {
				cleanup = typeof result === 'function' ? result : noop;

View on GitHub (pinned to 20b341f100)

Solutions

  1. Ensure every store in the array is a valid store (defined, with `subscribe`).
  2. Filter falsy members before passing: `stores.filter(Boolean)`.
  3. Provide defaults: `[a, b ?? readable(null)]`.

Example fix

// before
const d = derived([storeA, storeB], ([a, b]) => a + b); // storeB is undefined -> throws
// after
import { readable } from 'svelte/store';
const d = derived([storeA, storeB ?? readable(0)], ([a, b]) => a + b);
Defensive patterns

Strategy: validation

Validate before calling

function assertStores(stores) {
	const arr = Array.isArray(stores) ? stores : [stores];
	if (!arr.every((s) => s && typeof s.subscribe === 'function')) {
		throw new Error('derived() requires all inputs to be valid stores');
	}
}
assertStores([a, b]);
const d = derived([a, b], fn);

Type guard

function areAllStores(v) {
	const arr = Array.isArray(v) ? v : [v];
	return arr.every((s) => s != null && typeof s.subscribe === 'function');
}

Prevention

When it happens

Trigger: Passing an array with a falsy element: `derived([a, b], ...)` where `b` is `undefined`; passing a single falsy store: `derived(undefined, ...)`; a store variable that is conditionally defined or came from a failed import.

Common situations: Dynamic store arrays with an undefined member; refactoring that leaves a store unset; SSR/import issues yielding undefined; an array built from optional values without filtering.

Related errors


AI-assisted analysis of sveltejs/svelte@20b341f100 (2026-08-12). Data as JSON: /api/errors/4d69078ac9f163ea. Report an issue: GitHub.