{"record":{"id":"87445f8d7fdac148","repo":"jestjs/jest","slug":"jest-cannot-use-configuration-as-an-object-withou","errorCode":null,"errorMessage":"Jest: Cannot use configuration as an object without a file path.","messagePattern":"Jest: Cannot use configuration as an object without a file path\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/jest-config/src/index.ts","lineNumber":350,"sourceCode":" */\nexport async function readInitialOptions(\n  config?: string,\n  {\n    packageRootOrConfig = process.cwd(),\n    parentConfigDirname = null,\n    readFromCwd = false,\n    skipMultipleConfigError = false,\n  }: ReadJestConfigOptions = {},\n): Promise<{config: Config.InitialOptions; configPath: string | null}> {\n  if (typeof packageRootOrConfig !== 'string') {\n    if (parentConfigDirname) {\n      const rawOptions = packageRootOrConfig;\n      rawOptions.rootDir = rawOptions.rootDir\n        ? 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  }","sourceCodeStart":332,"sourceCodeEnd":368,"githubUrl":"https://github.com/jestjs/jest/blob/8e6d128e4a278059ecddecaa97400b04c8ae5fd9/packages/jest-config/src/index.ts#L332-L368","documentation":"readInitialOptions (index.ts:333-386) accepts either a string (path) or a config object as packageRootOrConfig. When given an object, it requires a parentConfigDirname (the directory of the parent config that referenced it) to resolve relative paths; without one, it throws (index.ts:349-353). The guard prevents an object config from being used with no anchor for <rootDir> resolution.","triggerScenarios":"Calling readConfig/readInitialOptions with a config object programmatically but not passing parentConfigDirname; a custom runner or tool that passes InitialOptions directly without a file context; calling readInitialOptions({...}) from cwd with no parent.","commonSituations":"Tooling that wraps jest-config and feeds an in-memory config object; programmatic API misuse where the caller assumed cwd would be implied; refactoring from a path string to an object without updating the second argument.","solutions":["Pass a path string (file or directory) instead of an object so Jest can resolve config from disk.","If you must pass an object, also pass parentConfigDirname pointing at a directory (e.g. process.cwd()).","Set rootDir explicitly in the object and supply a parentConfigDirname so replaceRootDirInPath works."],"exampleFix":"// before\nreadInitialOptions(undefined, { packageRootOrConfig: { testMatch: ['**/*.test.js'] } });\n// after\nreadInitialOptions(undefined, {\n  packageRootOrConfig: { testMatch: ['**/*.test.js'] },\n  parentConfigDirname: process.cwd(),\n});","handlingStrategy":"validation","validationCode":"function safeReadInitialOptions(opts: {\n  packageRootOrConfig?: string | object;\n  parentConfigDirname?: string | null;\n}) {\n  if (typeof opts.packageRootOrConfig !== 'string' && !opts.parentConfigDirname) {\n    throw new Error('Pass parentConfigDirname when supplying a config object');\n  }\n  return readInitialOptions(undefined, opts as any);\n}","typeGuard":"function isPathString(v: unknown): v is string {\n  return typeof v === 'string';\n}","tryCatchPattern":null,"preventionTips":["Prefer passing a config file path string over an in-memory object.","When using an object, always pass parentConfigDirname (typically process.cwd()).","Set rootDir explicitly in the object to make resolution deterministic."],"tags":["jest-config","readinitialoptions","config-object","rootdir"],"backgroundTag":null,"analyzedSha":"8e6d128e4a278059ecddecaa97400b04c8ae5fd9","analyzedAt":"2026-08-10T18:11:27.960Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}