sveltejs/kit · error · Error

called stringify before init_transport

Error message

called stringify before init_transport

What it means

stringify is a stub replaced by init_transport once SvelteKit's render pipeline sets up the devalue transport. Calling it before initialization throws in dev. This guards against serializing data outside the context where transport encoders are registered.

Source

Thrown at packages/kit/src/runtime/app/internal/transport.js:12

/** @import { Transport } from '@sveltejs/kit/hooks' */
import * as devalue from 'devalue';
import { DEV } from 'esm-env';

/** @type {(thing: any) => string} */
export let uneval = () => {
	throw new Error(DEV ? 'called uneval before init_transport' : '');
};

/** @type {(data: any) => string} */
export let stringify = () => {
	throw new Error(DEV ? 'called stringify before init_transport' : '');
};

/** @type {(data: string) => any} */
export let parse = () => {
	throw new Error(DEV ? 'called parse before init_transport' : '');
};

/** @type {Record<string, (data: any) => any>} */
export let encoders = {};

/** @type {Record<string, (data: any) => any>} */
export let decoders = {};

export let has_custom_transporters = false;

/**
 *
 * @param {Transport} transport

View on GitHub (pinned to 03f1687fe6)

Solutions

  1. Only serialize within kit's request lifecycle (load functions, hooks) where init_transport has already run
  2. Use the `devalue` package's stringify directly for standalone serialization
  3. If building a custom adapter/renderer, call init_transport before using stringify
  4. Update @sveltejs/kit to the latest version

Example fix

// before
import { stringify } from '@sveltejs/kit/internal';
// after
import { stringify } from 'devalue';
Defensive patterns

Strategy: try-catch

Validate before calling

import { stringify } from 'your-transport-module';
if (typeof encoders !== 'object' || Object.keys(encoders).length === 0) {
  throw new Error('transport not initialized: call init_transport first');
}

Try / catch

try {
  const json = stringify(data);
} catch (e) {
  if (e.message === 'called stringify before init_transport') {
    const json = devalueStringify(data); // fallback
  } else throw e;
}

Prevention

When it happens

Trigger: Invoking the exported stringify from the transport module (or code paths relying on it) before init_transport runs — e.g. custom server code, adapters, or tests calling serialization before kit's render setup.

Common situations: Importing internal transport utilities directly, custom adapters serializing data outside a kit-handled request, or unit tests without kit's pipeline initialization.

Related errors


AI-assisted analysis of sveltejs/kit@03f1687fe6 (2026-09-02). Data as JSON: /api/errors/c40676c0ea8ff716. Report an issue: GitHub.