parcel-bundler/parcel · error · ThrowableDiagnostic

Failed to parse package.json

Error message

Failed to parse package.json

What it means

Thrown by getConflictingLocalDependencies() in utils.js when JSON.parse() fails on the contents of a package.json file. The function reads the file via fs.readFile, attempts JSON.parse, and catches any SyntaxError to re-throw as a ThrowableDiagnostic. The TODO comment notes that codeframes are not yet attached to this diagnostic.

Source

Thrown at packages/core/package-manager/src/utils.js:57

export async function getConflictingLocalDependencies(
  fs: FileSystem,
  name: string,
  local: FilePath,
  projectRoot: FilePath,
): Promise<?{|json: string, filePath: FilePath, fields: Array<string>|}> {
  let pkgPath = await resolveConfig(fs, local, ['package.json'], projectRoot);
  if (pkgPath == null) {
    return;
  }

  let pkgStr = await fs.readFile(pkgPath, 'utf8');
  let pkg;
  try {
    pkg = JSON.parse(pkgStr);
  } catch (e) {
    // TODO: codeframe
    throw new ThrowableDiagnostic({
      diagnostic: {
        message: 'Failed to parse package.json',
        origin: '@parcel/package-manager',
      },
    });
  }

  if (typeof pkg !== 'object' || pkg == null) {
    // TODO: codeframe
    throw new ThrowableDiagnostic({
      diagnostic: {
        message: 'Expected package.json contents to be an object.',
        origin: '@parcel/package-manager',
      },
    });
  }

  let fields = [];

View on GitHub (pinned to 59484858a1)

Solutions

  1. Validate the package.json file with a JSON linter or `node -e "JSON.parse(require('fs').readFileSync('package.json','utf8'))"`.
  2. Fix the JSON syntax error — check for trailing commas, unquoted keys, or comments.
  3. Remove merge conflict markers if present (<<<<<<<, =======, >>>>>>>).
  4. Use an editor with JSON syntax checking to catch errors before saving.

Example fix

// before: invalid package.json
{
  "name": "myapp",
  "dependencies": {
    "lodash": "^4.0.0",
  },  // <-- trailing comma
}

// after: valid JSON
{
  "name": "myapp",
  "dependencies": {
    "lodash": "^4.0.0"
  }
}
Defensive patterns

Strategy: validation

Validate before calling

// Validate package.json is parseable JSON before use
const fs = require('fs');

function validatePackageJson(filePath) {
  let contents = fs.readFileSync(filePath, 'utf8');
  try {
    JSON.parse(contents);
  } catch (e) {
    throw new Error(`Invalid JSON in ${filePath}: ${e.message}`);
  }
}

// Run before Parcel build
validatePackageJson('./package.json');

Try / catch

try {
  await packageManager.resolve(id, from);
} catch (e) {
  if (e.diagnostics?.[0]?.message === 'Failed to parse package.json') {
    // Validate and fix the package.json file
    let {execSync} = require('child_process');
    execSync('node -e "JSON.parse(require(\'fs\').readFileSync(\'package.json\',\'utf8\'))"', {stdio: 'inherit'});
  } else {
    throw e;
  }
}

Prevention

When it happens

Trigger: resolveConfig finds a package.json, fs.readFile reads its contents as UTF-8, and JSON.parse throws a SyntaxError due to invalid JSON syntax (trailing commas, unquoted keys, single quotes, comments, etc.). The catch block re-wraps the error as a diagnostic with origin '@parcel/package-manager'.

Common situations: A trailing comma in package.json (common copy-paste error). Unquoted property keys. JSON5 syntax (comments, single quotes) in a file expected to be standard JSON. A BOM character or encoding issue causing a parse failure. Manual editing of package.json introducing syntax errors. Merge conflicts leaving conflict markers (<<<<<<<) in the file.

Understand the failure class

Related errors


AI-assisted analysis of parcel-bundler/parcel@59484858a1 (2026-08-13). Data as JSON: /api/errors/0c82f9d11e337ca2. Report an issue: GitHub.