sveltejs/kit · error · Error

Regular expressions are not valid remote function arguments

Error message

Regular expressions are not valid remote function arguments

What it means

Remote functions serialize their arguments to send to the server. RegExp is not JSON-serializable and can't be safely reconstructed, so a guard reducer throws when any RegExp appears in the arguments.

Source

Thrown at packages/kit/src/runtime/shared.js:102

// "sveltekit remote arg"
const remote_object = '__skrao';
const remote_map = '__skram';
const remote_set = '__skras';
const remote_file = '__skraf';
const remote_promise_guard = '__skrap';
const remote_regex_guard = '__skrag';
const remote_arg_marker = Symbol(remote_object);

/**
 * @param {boolean} sort
 */
function create_remote_arg_reducers(sort) {
	/** @type {Record<string, (value: unknown) => unknown>} */
	const remote_fns_reducers = {
		/** @param {unknown} value */
		[remote_regex_guard]: (value) => {
			if (value instanceof RegExp) {
				throw new Error('Regular expressions are not valid remote function arguments');
			}
		}
	};

	if (sort) {
		const clones = new Map();

		/** @type {(value: unknown) => Array<[unknown, unknown]> | undefined} */
		remote_fns_reducers[remote_map] = (value) => {
			if (!(value instanceof Map)) {
				return;
			}

			/** @type {Array<[string, string]>} */
			const entries = [];

			for (const [key, val] of value) {
				entries.push([stringify(key), stringify(val)]);

View on GitHub (pinned to 03f1687fe6)

Solutions

  1. Pass the pattern string and flags separately and construct the RegExp on the server
  2. Serialize as a plain object: { source: re.source, flags: re.flags } and rebuild remotely
  3. Strip RegExp values from your arguments before invoking the remote function

Example fix

// before
const results = await search(new RegExp(term, 'i'));
// after
const results = await search({ source: term, flags: 'i' }); // server: new RegExp(source, flags)
Defensive patterns

Strategy: validation

Validate before calling

function assertNoRegExp(value, seen = new Set()) {
  if (value instanceof RegExp) throw new Error('RegExp found in remote args');
  if (value && typeof value === 'object' && !seen.has(value)) {
    seen.add(value);
    Object.values(value).forEach((v) => assertNoRegExp(v, seen));
  }
}
// call on arguments before invoking remote function

Type guard

function isRegExp(v) { return v instanceof RegExp; }
// filter: if (args.some(isRegExp)) rebuild args as { source, flags };

Try / catch

try {
  await remoteSearch(args);
} catch (e) {
  if (e.message.includes('Regular expressions are not valid')) {
    console.error('Convert RegExp args to { source, flags }');
  }
}

Prevention

When it happens

Trigger: Calling a remote function (e.g. 'use server' query/command) with an argument that is a RegExp or contains one, like searchRegex passed as a filter.

Common situations: Reusing client-side search code that builds RegExp from user input and passing it to the remote function instead of the raw pattern string.

Related errors


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