sveltejs/kit · error · Error

Invalid BODY_SIZE_LIMIT: '${env('BODY_SIZE_LIMIT')}'. Please

Error message

Invalid BODY_SIZE_LIMIT: '${env('BODY_SIZE_LIMIT')}'. Please provide a numeric value.

What it means

adapter-node parses BODY_SIZE_LIMIT into a byte count via parse_as_bytes (e.g. '512K'). If the resulting value is NaN the server refuses to start because an unbounded/invalid body limit cannot be enforced safely.

Source

Thrown at packages/adapter-node/src/handler.js:30

import { parse_as_bytes } from './utils.js';

/** @typedef {(req: IncomingMessage, res: ServerResponse, next: () => void | Promise<void>) => void | Promise<void>} Middleware */

const origin = ORIGIN;
const uncompressed_extensions = UNCOMPRESSED_EXTENSIONS;
const prerendered = PRERENDERED;
const mime_types = MIME_TYPES;

const xff_depth = parseInt(env('XFF_DEPTH', '1'));
const address_header = env('ADDRESS_HEADER', '').toLowerCase();
const protocol_header = env('PROTOCOL_HEADER', '').toLowerCase();
const host_header = env('HOST_HEADER', '').toLowerCase();
const port_header = env('PORT_HEADER', '').toLowerCase();

const body_size_limit = parse_as_bytes(env('BODY_SIZE_LIMIT', '512K'));

if (isNaN(body_size_limit)) {
	throw new Error(
		`Invalid BODY_SIZE_LIMIT: '${env('BODY_SIZE_LIMIT')}'. Please provide a numeric value.`
	);
}

const asset_dir = `${dir}/client${BASE_PATH}`;

await server.init({
	env: process.env,
	read: (file) => createReadableStream(`${asset_dir}/${file}`)
});

/**
 * @param {string} path
 * @param {boolean} client
 */
function serve(path, client = false) {
	return fs.existsSync(path)
		? sirv(path, {

View on GitHub (pinned to 03f1687fe6)

Solutions

  1. Use a valid size literal like '512K', '4M', or a plain byte count like 524288
  2. Check the value for stray spaces/quotes in your .env or deployment config
  3. If using bytes, set a plain integer, e.g. BODY_SIZE_LIMIT=524288

Example fix

// before
BODY_SIZE_LIMIT=5 MB
// after
BODY_SIZE_LIMIT=5M
Defensive patterns

Strategy: validation

Validate before calling

const raw = process.env.BODY_SIZE_LIMIT;
if (raw !== undefined && isNaN(parseAsBytes(raw))) {
  throw new Error(`BODY_SIZE_LIMIT must be a size like '512K' or bytes, got: ${raw}`);
}

Type guard

function isValidBodySize(v) {
  return /^\d+(K|M|G)?$/i.test(String(v).trim());
}

Try / catch

try {
  start();
} catch (err) {
  if (String(err.message).includes('Invalid BODY_SIZE_LIMIT')) {
    console.error('Set BODY_SIZE_LIMIT to e.g. 512K, 4M, or a byte count');
    process.exit(1);
  }
  throw err;
}

Prevention

When it happens

Trigger: Setting BODY_SIZE_LIMIT to a string that parse_as_bytes cannot convert to a number, e.g. '5 MB', 'abc', or an empty string, in handler.js at startup.

Common situations: Using unit spellings with spaces or unsupported suffixes ('512 KiB'), decimal values, or copy-pasting a value with hidden characters from docs/CI.

Related errors


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