{"record":{"id":"f2278f484d08d447","repo":"pnpm/pnpm","slug":"pkg-set-json-parse","errorCode":"PKG_SET_JSON_PARSE","errorMessage":"Failed to parse value as JSON: \"${value as string}\"","messagePattern":"Failed to parse value as JSON: \"(.+?)\"","errorType":"validation","errorClass":"PnpmError","httpStatus":null,"severity":"error","filePath":"pnpm11/pkg-manifest/commands/src/pkg.ts","lineNumber":146,"sourceCode":"\n  const { manifest, writeProjectManifest } = await readProjectManifest(opts.dir)\n\n  for (const arg of args) {\n    const eqIndex = arg.indexOf('=')\n    if (eqIndex === -1) {\n      throw new PnpmError('PKG_SET_INVALID_ARG', `Invalid argument \"${arg}\". Expected key=value format`, {\n        hint: 'Example: pnpm pkg set name=my-package',\n      })\n    }\n\n    const key = arg.slice(0, eqIndex)\n    let value: unknown = arg.slice(eqIndex + 1)\n\n    if (opts.json) {\n      try {\n        value = JSON.parse(value as string)\n      } catch {\n        throw new PnpmError('PKG_SET_JSON_PARSE', `Failed to parse value as JSON: \"${value as string}\"`)\n      }\n    }\n\n    setObjectValueByPropertyPathString(manifest as unknown as Record<string, unknown>, key, value)\n  }\n\n  await writeProjectManifest(manifest)\n}\n\nasync function pkgDelete (opts: PkgCommandOptions, args: string[]): Promise<void> {\n  if (args.length === 0) {\n    throw new PnpmError('PKG_DELETE_MISSING_ARGS', 'Missing keys to delete', {\n      hint: help(),\n    })\n  }\n\n  const { manifest, writeProjectManifest } = await readProjectManifest(opts.dir)\n","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/pnpm/pnpm/blob/6261b7f388016d57ca6b90340342411cd1d0d00f/pnpm11/pkg-manifest/commands/src/pkg.ts#L128-L164","documentation":"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.","triggerScenarios":"`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).","commonSituations":"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.","solutions":["Single-quote the whole token so the shell preserves the inner double quotes: `pnpm pkg set --json 'config={\"a\":1}'`","Sanity-check the value first: `echo '<value>' | jq .` must succeed","Drop --json when only a string value is needed - `pnpm pkg set name=value` never parses"],"exampleFix":"# before -- shell strips the quotes, value is not valid JSON\npnpm pkg set --json config={\"a\":1}\n\n# after -- single-quote the whole token\npnpm pkg set --json 'config={\"a\":1}'","handlingStrategy":"validation","validationCode":"const value = arg.slice(arg.indexOf('=') + 1)\ntry {\n  JSON.parse(value)\n} catch {\n  throw new Error(`--json value is not valid JSON: ${value}`)\n}","typeGuard":null,"tryCatchPattern":"try {\n  JSON.parse(value)\n} catch (err) {\n  if (err instanceof SyntaxError) {\n    throw new Error(`--json value is not valid JSON (${err.message}): ${value}`)\n  }\n  throw err\n}","preventionTips":["Single-quote the entire key=value token on POSIX shells so inner double quotes survive","Validate values with JSON.parse or `jq .` before passing them to `pnpm pkg set --json`","Remember JSON requires double quotes, no trailing commas, and no comments - JS object literals are rejected"],"tags":["pnpm-pkg","json","quoting","set","cli"],"backgroundTag":"json-parse-error","analyzedSha":"6261b7f388016d57ca6b90340342411cd1d0d00f","analyzedAt":"2026-08-17T18:30:54.750Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}