jestjs/jest · error · Error
Cannot parse ${filePath} as JSON: ${error.message}
Error message
Cannot parse ${filePath} as JSON: ${error.message} What it means
The haste-map worker reads each file ending in package.json and JSON.parses its contents to extract the package `name`. If parsing fails it wraps the underlying SyntaxError into a clear Error naming the offending file path, so a single corrupt package.json does not produce an opaque JSON parse stack. The throw is in worker.ts:60 inside the catch of the JSON.parse try block.
Source
Thrown at packages/jest-haste-map/src/worker.ts:60
if (content === undefined) {
content = fs.readFileSync(filePath, 'utf8');
}
return content;
};
if (filePath.endsWith(PACKAGE_JSON)) {
// Process a package.json that is returned as a PACKAGE type with its name.
try {
const fileData = JSON.parse(getContent());
if (fileData.name) {
const relativeFilePath = path.relative(rootDir, filePath);
id = fileData.name;
module = [relativeFilePath, H.PACKAGE];
}
} catch (error: any) {
throw new Error(`Cannot parse ${filePath} as JSON: ${error.message}`);
}
} else if (!blacklist.has(filePath.slice(filePath.lastIndexOf('.')))) {
// Process a random file that is returned as a MODULE.
if (hasteImpl) {
id = hasteImpl.getHasteName(filePath);
}
if (computeDependencies) {
const content = getContent();
const extractor = data.dependencyExtractor
? await requireOrImportModule<DependencyExtractor>(
data.dependencyExtractor,
false,
)
: defaultDependencyExtractor;
dependencies = [
...extractor.extract(
content,View on GitHub (pinned to f49721c78e)
Solutions
- Open the path in the error message and validate it with a JSON linter or `node -e "JSON.parse(require('fs').readFileSync('PATH','utf8'))"`.
- Remove JSON-incompatible syntax (comments, trailing commas, unquoted keys) or rename the file so haste-map does not treat it as package.json.
- Strip a leading UTF-8 BOM if present.
- If the file is intentionally JSON5, give it a different name so it is not discovered as a package manifest.
Example fix
// before — package.json with a trailing comma (invalid JSON)
{
"name": "my-pkg",
"version": "1.0.0",
}
// after — valid JSON
{
"name": "my-pkg",
"version": "1.0.0"
} Defensive patterns
Strategy: validation
Validate before calling
import {readFileSync} from 'node:fs';
function assertValidPackageJson(filePath: string) {
if (!filePath.endsWith('package.json')) return;
const txt = readFileSync(filePath, 'utf8');
try { JSON.parse(txt); }
catch (e) { throw new Error(`${filePath} is not valid JSON: ${(e as Error).message}`); }
} Try / catch
try {
JSON.parse(content);
} catch (err) {
throw new Error(`Cannot parse ${filePath} as JSON: ${(err as Error).message}`);
} Prevention
- Lint all package.json files in pre-commit (e.g. jsonlint).
- Avoid JSON5/JSONC content in files named package.json — rename if needed.
- Validate package.json after programmatic generation/edits.
When it happens
Trigger: worker() detects filePath.endsWith(PACKAGE_JSON) (worker.ts:49), reads the file content via graceful-fs, and JSON.parse(getContent()) throws — e.g. trailing commas, comments, BOM, truncation, or a JSON5 file misnamed package.json. The catch at line 59 rethrows as `Cannot parse <filePath> as JSON: <error.message>`.
Common situations: A package.json edited by hand with a trailing comma or comment; a package.json written as JSON5/JSONC; a corrupted/partially-written file from a crashed install; symlinked package.json whose target is empty; BOM-prefixed file from a Windows tool.
Related errors
- There is malformed json in ${packageJsonPath}
- Could not find a "package.json" file in ${rootDir}
- There was an error while parsing the `--config` argument as
- Configuration in ${packageJson} is not valid. Jest expects t
- Crawler retry failed: Original error: ${retryError.message
AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03).
Data as JSON: /data/errors/7b9019af1116773c.json.
Report an issue: GitHub.