sveltejs/svelte · error · Error

state_prototype_fixed

state_prototype_fixed

Error message

state_prototype_fixed
Cannot set prototype of `$state` object
https://svelte.dev/e/state_prototype_fixed

What it means

Thrown at internal/client/proxy.js:351 in the Proxy setPrototypeOf trap of a $state object. Svelte's reactivity and forking rely on the state object's prototype staying fixed; changing the prototype via Object.setPrototypeOf would invalidate the internal signal bookkeeping and Proxy invariants, so it is unconditionally blocked.

Solutions

  1. Create the object with the desired prototype first, then wrap it in $state: `const obj = Object.create(proto); const state = $state(obj)`.
  2. Store methods separately (e.g. on a class instance kept in $state) rather than reassigning prototypes.
  3. Use composition: keep data in $state and behaviour in plain functions that take the state as an argument.

Example fix

// before
const state = $state({});
Object.setPrototypeOf(state, methodsProto); // throws

// after
const obj = Object.create(methodsProto);
const state = $state(obj);
Defensive patterns

Strategy: validation

Validate before calling

// Never call Object.setPrototypeOf on a $state object.
// Create the object with the prototype first, then wrap in $state:
// const obj = Object.create(proto); const state = $state(obj);

Prevention

When it happens

Trigger: Calling Object.setPrototypeOf(stateObj, newProto) where stateObj is a $state-proxied object (e.g. `$state({})` or `$state(array)`). The trap at proxy.js:350-352 always throws.

Common situations: Assigning a class prototype onto a plain state object to add methods; mixing prototype-based inheritance with runes state; libraries that call setPrototypeOf for mixin patterns; attempted prototype pollution.

Related errors


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

Appendix: source

Thrown at packages/svelte/src/internal/client/errors.js:476

		throw error;
	} else {
		throw new Error(`https://svelte.dev/e/state_descriptors_fixed`);
	}
}

/**
 * Cannot set prototype of `$state` object
 * @returns {never}
 */
export function state_prototype_fixed() {
	if (DEV) {
		const error = new Error(`state_prototype_fixed\nCannot set prototype of \`$state\` object\nhttps://svelte.dev/e/state_prototype_fixed`);

		error.name = 'Svelte error';

		throw error;
	} else {
		throw new Error(`https://svelte.dev/e/state_prototype_fixed`);
	}
}

/**
 * Updating state inside `$derived(...)`, `$inspect(...)` or a template expression is forbidden. If the value should not be reactive, declare it without `$state`
 * @returns {never}
 */
export function state_unsafe_mutation() {
	if (DEV) {
		const error = new Error(`state_unsafe_mutation\nUpdating state inside \`$derived(...)\`, \`$inspect(...)\` or a template expression is forbidden. If the value should not be reactive, declare it without \`$state\`\nhttps://svelte.dev/e/state_unsafe_mutation`);

		error.name = 'Svelte error';

		throw error;
	} else {
		throw new Error(`https://svelte.dev/e/state_unsafe_mutation`);
	}
}

View on GitHub (pinned to 20b341f100)