mastra-ai/mastra · error · Error
Invalid JSON in presets file: ${presetsPath}
Error message
Invalid JSON in presets file: ${presetsPath} What it means
After reading the presets file, loadAndValidatePresets() runs JSON.parse and throws this error if parsing fails. The file exists but its contents are not valid JSON. Note the message uses the original (possibly relative) presetsPath argument, not the absolute path.
Source
Thrown at packages/cli/src/utils/validate-presets.ts:25
*
* @param presetsPath - Path to the presets JSON file (relative or absolute)
* @returns The original JSON string content
* @throws Error if file doesn't exist, JSON is invalid, or structure is incorrect
*/
export async function loadAndValidatePresets(presetsPath: string): Promise<string> {
const absolutePath = resolve(process.cwd(), presetsPath);
if (!existsSync(absolutePath)) {
throw new Error(`Presets file not found: ${absolutePath}`);
}
const content = await readFile(absolutePath, 'utf-8');
let parsed: unknown;
try {
parsed = JSON.parse(content);
} catch {
throw new Error(`Invalid JSON in presets file: ${presetsPath}`);
}
if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
throw new Error(`Presets file must contain a JSON object with named presets`);
}
// Validate each preset value is an object
for (const [key, value] of Object.entries(parsed)) {
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
throw new Error(`Preset "${key}" must be a JSON object`);
}
}
return content; // Return original string to preserve formatting
}
View on GitHub (pinned to 75dd419e61)
Solutions
- Run the file through a JSON parser to find the syntax error: `node -e "JSON.parse(require('fs').readFileSync(process.argv[1],'utf8'))" <path>` or a linter.
- Remove JSON-incompatible syntax: trailing commas, comments, single quotes, unquoted keys.
- Strip a UTF-8 BOM if present and ensure the file is UTF-8 encoded.
Example fix
// before (presets.json)
{
"default": { "temperature": 0.7, }, // trailing comma + comment
}
// after
{
"default": { "temperature": 0.7 }
} Defensive patterns
Strategy: validation
Validate before calling
function assertValidJsonFile(path: string): boolean {
try {
JSON.parse(require('node:fs').readFileSync(path, 'utf8'));
return true;
} catch {
return false;
}
} Try / catch
try {
await loadAndValidatePresets(presetsPath);
} catch (err) {
if ((err as Error).message.startsWith('Invalid JSON in presets file')) {
const raw = require('node:fs').readFileSync(presetsPath, 'utf8').replace(/^\uFEFF/, '');
try { JSON.parse(raw); } catch (e) { console.error(`JSON syntax error at ${(e as Error).message}`); }
process.exit(1);
}
throw err;
} Prevention
- Lint presets files with a JSON parser/CI step before running the CLI.
- Avoid hand-editing: generate the file programmatically with JSON.stringify.
- Save files as UTF-8 without BOM and never add comments/trailing commas.
When it happens
Trigger: `mastra dev`/`mastra studio` pointed at a presets file containing syntax errors: trailing commas, single quotes, comments (JSONC), unquoted keys, truncated content, or a file saved in a non-UTF-8/BOM-encoded format.
Common situations: Hand-editing the presets file and leaving a trailing comma, pasting JSON5/JSONC config with comments, a partially written file from a crashed process, or a template placeholder (${...}) left un-substituted.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- Could not parse ${PLUGIN_MANIFEST_FILE}: ${error instanceof
- Presets file must contain a JSON object with named presets
- Preset "${key}" must be a JSON object
- MISSING_INPUT
- INVALID_JSON
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/e7fef588cafbe5c8.
Report an issue: GitHub.