{"id":"81919eb4cbccbc91","repo":"babel/babel","slug":"expected-config-object-but-found-array-81919e","errorCode":null,"errorMessage":"Expected config object but found array","messagePattern":"Expected config object but found array","errorType":"exception","errorClass":"ConfigError","httpStatus":null,"severity":"critical","filePath":"packages/babel-core/src/config/files/package.ts","lineNumber":32,"sourceCode":"    try {\n      options = JSON.parse(content) as unknown;\n    } catch (err) {\n      throw new ConfigError(\n        `Error while parsing JSON - ${err.message}`,\n        filepath,\n      );\n    }\n\n    if (!options) throw new Error(`${filepath}: No config detected`);\n\n    if (typeof options !== \"object\") {\n      throw new ConfigError(\n        `Config returned typeof ${typeof options}`,\n        filepath,\n      );\n    }\n    if (Array.isArray(options)) {\n      throw new ConfigError(`Expected config object but found array`, filepath);\n    }\n\n    return {\n      filepath,\n      dirname: path.dirname(filepath),\n      options,\n    };\n  },\n);\n\n/**\n * Find metadata about the package that this file is inside of. Resolution\n * of Babel's config requires general package information to decide when to\n * search for .babelrc files\n */\nexport function* findPackageData(filepath: string): Handler<FilePackageData> {\n  let pkg = null;\n  const directories = [];","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/babel/babel/blob/06b6eae39da4ce0689fad64e2c48a6375a464208/packages/babel-core/src/config/files/package.ts#L14-L50","documentation":"Thrown by Babel's package.json reader (makeStaticFileCache callback in package.ts) after JSON.parse succeeds but the resulting value is a JSON array. A package.json file must be a JSON object at the top level; an array is structurally invalid and Babel cannot derive package metadata (dirname, babelrc search boundaries) from it. The check guards the very first step of config resolution, where Babel walks up directories looking for the owning package.","triggerScenarios":"Babel calls findPackageData -> readConfigPackage while locating the package that owns a file being compiled. If the nearest package.json parses to a JSON array (e.g. file content is literally `[]` or `[\"a\",\"b\"]`), the Array.isArray(options) branch at package.ts:31 fires and a ConfigError wrapping this message is thrown with the offending filepath.","commonSituations":"A package.json was overwritten or corrupted (e.g. a tool wrote a JSON array of dependency names into it), a symlink points at the wrong file, a monorepo hoisting accident left a stub package.json containing an array, or a hand-edited/package-generator script emitted malformed JSON. Also seen when a config file masquerading as package.json is mistakenly named package.json.","solutions":["Open the package.json named in the ConfigError filepath and confirm its top-level value is an object `{ ... }`, not an array.","Restore package.json from version control or regenerate it via `npm init` if the contents are unrecoverable.","Run the file through a JSON linter (`npx jsonlint package.json`) to catch structural corruption.","Check for stray symlinks or monorepo hoisting that may have replaced the real package.json with another file."],"exampleFix":"// before: package.json contains\n[\"@babel/core\", \"webpack\"]\n// after: package.json contains\n{\n  \"name\": \"my-package\",\n  \"version\": \"1.0.0\"\n}","handlingStrategy":"validation","validationCode":"const fs = require('fs');\nfunction validatePackageJson(filepath) {\n  const parsed = JSON.parse(fs.readFileSync(filepath, 'utf8'));\n  if (Array.isArray(parsed)) throw new Error(`${filepath}: package.json must be an object, got array`);\n  if (parsed === null || typeof parsed !== 'object') throw new Error(`${filepath}: package.json must be an object`);\n  return parsed;\n}","typeGuard":"function isValidPackageJson(v): v is Record<string, unknown> {\n  return v !== null && typeof v === 'object' && !Array.isArray(v);\n}","tryCatchPattern":"try {\n  babel.transformSync(code, { filename });\n} catch (e) {\n  if (e.message.includes('Expected config object but found array')) {\n    console.error('package.json is corrupt; restore from VCS:', e.message);\n  } else throw e;\n}","preventionTips":["Lint package.json in CI with jsonlint or prettier.","Never programmatically overwrite package.json with an array.","Add a pre-commit hook that validates package.json structure."],"tags":["config","package-json","json-parse","config-resolution"],"analyzedSha":"06b6eae39da4ce0689fad64e2c48a6375a464208","analyzedAt":"2026-08-03T20:13:43.465Z","schemaVersion":2}