pnpm/pnpm · error · PnpmError

PKG_SET_JSON_PARSE

PKG_SET_JSON_PARSE

Error message

Failed to parse value as JSON: "${value as string}"

What it means

With `--json`, `pnpm pkg set` runs `JSON.parse` on the value part after `=` and stores the parsed structure in the manifest. The value must be strict JSON; a parse failure throws PKG_SET_JSON_PARSE echoing the offending value. Without `--json` the value is stored as a plain string and this error cannot occur.

Source

Thrown at pnpm11/pkg-manifest/commands/src/pkg.ts:146

  const { manifest, writeProjectManifest } = await readProjectManifest(opts.dir)

  for (const arg of args) {
    const eqIndex = arg.indexOf('=')
    if (eqIndex === -1) {
      throw new PnpmError('PKG_SET_INVALID_ARG', `Invalid argument "${arg}". Expected key=value format`, {
        hint: 'Example: pnpm pkg set name=my-package',
      })
    }

    const key = arg.slice(0, eqIndex)
    let value: unknown = arg.slice(eqIndex + 1)

    if (opts.json) {
      try {
        value = JSON.parse(value as string)
      } catch {
        throw new PnpmError('PKG_SET_JSON_PARSE', `Failed to parse value as JSON: "${value as string}"`)
      }
    }

    setObjectValueByPropertyPathString(manifest as unknown as Record<string, unknown>, key, value)
  }

  await writeProjectManifest(manifest)
}

async function pkgDelete (opts: PkgCommandOptions, args: string[]): Promise<void> {
  if (args.length === 0) {
    throw new PnpmError('PKG_DELETE_MISSING_ARGS', 'Missing keys to delete', {
      hint: help(),
    })
  }

  const { manifest, writeProjectManifest } = await readProjectManifest(opts.dir)

View on GitHub (pinned to 6261b7f388)

Solutions

  1. Single-quote the whole token so the shell preserves the inner double quotes: `pnpm pkg set --json 'config={"a":1}'`
  2. Sanity-check the value first: `echo '<value>' | jq .` must succeed
  3. Drop --json when only a string value is needed - `pnpm pkg set name=value` never parses

Example fix

# before -- shell strips the quotes, value is not valid JSON
pnpm pkg set --json config={"a":1}

# after -- single-quote the whole token
pnpm pkg set --json 'config={"a":1}'
Defensive patterns

Strategy: validation

Validate before calling

const value = arg.slice(arg.indexOf('=') + 1)
try {
  JSON.parse(value)
} catch {
  throw new Error(`--json value is not valid JSON: ${value}`)
}

Try / catch

try {
  JSON.parse(value)
} catch (err) {
  if (err instanceof SyntaxError) {
    throw new Error(`--json value is not valid JSON (${err.message}): ${value}`)
  }
  throw err
}

Prevention

When it happens

Trigger: `pnpm pkg set --json config={a:1}` (JS object literal, not JSON); unquoted JSON where the shell strips or splits the inner double quotes; trailing commas, single-quoted keys, or comments inside the value; an empty value with --json (`key=` tries to parse an empty string).

Common situations: Writing a JavaScript object literal where JSON is required; POSIX shell quoting eating the inner double quotes; copying a value from JS source that uses single quotes.

Understand the failure class

Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.

Related errors


AI-assisted analysis of pnpm/pnpm@6261b7f388 (2026-08-17). Data as JSON: /api/errors/f2278f484d08d447. Report an issue: GitHub.