{"id":"149fcc143c07883f","repo":"jestjs/jest","slug":"there-was-an-error-while-parsing-the-config-ar","errorCode":null,"errorMessage":"There was an error while parsing the `--config` argument as a JSON string.","messagePattern":"There was an error while parsing the `--config` argument as a JSON string\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/jest-config/src/index.ts","lineNumber":364,"sourceCode":"        ? replaceRootDirInPath(parentConfigDirname, rawOptions.rootDir)\n        : parentConfigDirname;\n      return {config: rawOptions, configPath: null};\n    } else {\n      throw new Error(\n        'Jest: Cannot use configuration as an object without a file path.',\n      );\n    }\n  }\n  if (isJSONString(config)) {\n    try {\n      // A JSON string was passed to `--config` argument and we can parse it\n      // and use as is.\n      const initialOptions = JSON.parse(config);\n      // NOTE: we might need to resolve this dir to an absolute path in the future\n      initialOptions.rootDir = initialOptions.rootDir || packageRootOrConfig;\n      return {config: initialOptions, configPath: null};\n    } catch {\n      throw new Error(\n        'There was an error while parsing the `--config` argument as a JSON string.',\n      );\n    }\n  }\n  if (!readFromCwd && typeof config == 'string') {\n    // A string passed to `--config`, which is either a direct path to the config\n    // or a path to directory containing `package.json`, `jest.config.js` or `jest.config.ts`\n    const configPath = resolveConfigPath(\n      config,\n      process.cwd(),\n      skipMultipleConfigError,\n    );\n    return {config: await readConfigFileAndSetRootDir(configPath), configPath};\n  }\n  // Otherwise just try to find config in the current rootDir.\n  const configPath = resolveConfigPath(\n    packageRootOrConfig,\n    process.cwd(),","sourceCodeStart":346,"sourceCodeEnd":382,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/jest-config/src/index.ts#L346-L382","documentation":"`readInitialOptions` (packages/jest-config/src/index.ts:355) treats a `--config` value that looks like JSON (passes isJSONString) by running `JSON.parse` on it; if parsing throws, Jest rethrows a generic error. This means the string started with `{`/`[` (so it was classified as JSON) but contained a syntax error.","triggerScenarios":"`jest --config '{testMatch: \"**/*.test.js\"}'` (unquoted keys are invalid JSON), trailing commas, single-quoted strings, or a malformed inline JSON object passed via CLI/shell.","commonSituations":"Shell quoting that strips inner quotes; hand-writing a JSON config inline rather than using a file; passing a JS object literal where JSON is required.","solutions":["Use a config FILE (jest.config.js/ts/json) and pass its path instead of an inline JSON string.","If inlining JSON, ensure valid JSON syntax: double-quoted keys and strings, no trailing commas, no comments.","Shell-escape the JSON correctly (single-quote the whole string on POSIX shells)."],"exampleFix":"# before (invalid JSON: unquoted keys)\njest --config '{testMatch: \"**/*.test.js\"}'\n\n# after (valid JSON)\njest --config '{\"testMatch\": [\"**/*.test.js\"]}'\n# or, preferred:\njest --config ./jest.config.js","handlingStrategy":"try-catch","validationCode":"function tryParseInlineConfig(cfg: string): object {\n  try { return JSON.parse(cfg); }\n  catch { throw new Error('--config is not valid JSON; use a config file or fix the JSON'); }\n}","typeGuard":"const looksLikeJson = (s: string) => {\n  const t = s.trim();\n  return t.startsWith('{') || t.startsWith('[');\n};","tryCatchPattern":"try {\n  JSON.parse(argv.config);\n} catch {\n  // fall back to treating --config as a file path instead of inline JSON\n  argv.config = resolveConfigFilePath(argv.config);\n}","preventionTips":["Prefer config files over inline JSON on the CLI.","Validate JSON in a scratch `JSON.parse` before invoking Jest.","Quote inline JSON correctly in shells."],"tags":["jest-config","config","json","cli-args"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}