sveltejs/kit · error · Error

Malformed tsconfig ${JSON.stringify(error, null, 2)}

Error message

Malformed tsconfig
${JSON.stringify(error, null, 2)}

What it means

After locating the config file, `load_tsconfig` calls `ts.readConfigFile`, which reports syntax/JSON errors in its `error` result. The packager surfaces this as a 'Malformed tsconfig' error with the diagnostics JSON appended, since a broken config cannot be parsed for compiler options.

Source

Thrown at packages/package/src/typescript.js:290

				config_filename = tsconfig;
				break;
			}

			if (fs.existsSync(jsconfig)) {
				config_filename = jsconfig;
				break;
			}
		}
	}

	if (!config_filename) {
		throw new Error('Failed to locate tsconfig or jsconfig');
	}

	const { error, config } = ts.readConfigFile(config_filename, ts.sys.readFile);

	if (error) {
		throw new Error('Malformed tsconfig\n' + JSON.stringify(error, null, 2));
	}

	// Do this so TS will not search for initial files which might take a while
	config.include = [];
	config.files = [];
	const { options } = ts.parseJsonConfigFileContent(
		config,
		ts.sys,
		path.dirname(config_filename),
		{ sourceMap: false },
		config_filename
	);

	for (const dir of traversed_dirs) {
		cache.set(dir, options);
	}

	return options;

View on GitHub (pinned to 03f1687fe6)

Solutions

  1. Read the diagnostics JSON in the message and fix the reported line/character in the tsconfig.
  2. Validate the file with `npx tsc -p <path> --noEmit` or an editor's JSONC linter.
  3. Remove merge conflict markers or restore the file from version control.
  4. Check for a BOM or encoding corruption; resave as clean UTF-8.

Example fix

// before (tsconfig.json)
{
  "compilerOptions": {
    "target": "ES2022" // <- malformed: unquoted trailing garbage
    module: "ESNext",
  }
}

// after
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "ESNext"
  }
}
Defensive patterns

Strategy: validation

Validate before calling

import fs from 'node:fs';
const src = fs.readFileSync('tsconfig.json', 'utf8').replace(/^\uFEFF/, '');
try { JSON.parse(src.replace(/\/\/.*$/gm, '').replace(/,\s*([}\]])/g, '$1')); } catch (e) { throw new Error('tsconfig.json has invalid syntax: ' + e.message); }

Try / catch

try {
  await run(['svelte-package']);
} catch (e) {
  if (String(e.message).startsWith('Malformed tsconfig')) {
    // message embeds ts diagnostics JSON — surface it verbatim
    throw e;
  }
  throw e;
}

Prevention

When it happens

Trigger: tsconfig.json/jsconfig.json contains invalid JSONC — trailing commas in wrong places, unquoted keys, stray BOM/characters, comment or syntax typos that the TS parser rejects.

Common situations: Hand-edited tsconfig with a typo; merge conflict markers left in the file; JSON5/JSONC syntax not accepted by the TS parser; truncated config after a failed edit.

Understand the failure class

Related errors


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