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
- Ensure every store in the array is a valid store (defined, with `subscribe`).
- Filter falsy members before passing: `stores.filter(Boolean)`.
- 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
- Filter falsy members before passing arrays to `derived`: `stores.filter(Boolean)`.
- Provide defaults: `store ?? readable(initial)`.
- Check store imports resolve (not undefined) at module load.
- Validate store arrays in dev before subscribing.
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
- async_derived_orphan
- derived_references_self
- effect_in_unowned_derived
- get_abort_signal_outside_reaction
- state_unsafe_mutation
AI-assisted analysis of sveltejs/svelte@20b341f100 (2026-08-12).
Data as JSON: /api/errors/4d69078ac9f163ea.
Report an issue: GitHub.