{"id":"d680cd4be150853a","repo":"babel/babel","slug":"config-returned-typeof-typeof-options-d680cd","errorCode":null,"errorMessage":"Config returned typeof ${typeof options}","messagePattern":"Config returned typeof (.+?)","errorType":"exception","errorClass":"ConfigError","httpStatus":null,"severity":"error","filePath":"packages/babel-core/src/config/files/package.ts","lineNumber":26,"sourceCode":"\nconst PACKAGE_FILENAME = \"package.json\";\n\nconst readConfigPackage = makeStaticFileCache(\n  (filepath, content): ConfigFile => {\n    let options;\n    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","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/babel/babel/blob/06b6eae39da4ce0689fad64e2c48a6375a464208/packages/babel-core/src/config/files/package.ts#L8-L44","documentation":"readConfigPackage parses a package.json and then verifies the result is an object. If JSON.parse succeeds but yields a non-object primitive (string, number, boolean), Babel throws ConfigError 'Config returned typeof ${typeof options}'. A package.json root must be a JSON object; a scalar value is invalid package metadata.","triggerScenarios":"A package.json whose entire content is a quoted string (\"my-package\"), a number (42), or a boolean (true). These are valid JSON primitives but not valid package manifests.","commonSituations":"Accidentally overwriting package.json with a scalar (e.g. writing only the package name); tooling that writes a single value; misgenerated file.","solutions":["Restore package.json to a proper JSON object with name/version and dependencies.","Run npm init to regenerate a valid package.json skeleton.","Add a pre-build validation that asserts typeof require('./package.json') === 'object'."],"exampleFix":"// before - package.json throws error 39\n\"my-package\"\n\n// after\n{\n  \"name\": \"my-package\",\n  \"version\": \"1.0.0\"\n}","handlingStrategy":"type-guard","validationCode":"const fs = require('fs');\nfunction checkPackageType(dir) {\n  const file = require('path').join(dir, 'package.json');\n  if (!fs.existsSync(file)) return;\n  const parsed = JSON.parse(fs.readFileSync(file, 'utf8'));\n  if (typeof parsed !== 'object' || parsed === null) {\n    throw new Error(`${file}: package.json must be an object, got ${typeof parsed}`);\n  }\n}","typeGuard":"function isPackageObject(value: unknown): value is { name?: string; version?: string; [k: string]: unknown } {\n  return typeof value === 'object' && value !== null && !Array.isArray(value);\n}","tryCatchPattern":"try { babel.transformFileSync(file); }\ncatch (err) {\n  if (/Config returned typeof/.test(err.message) && /package\\.json/.test(err.filename || '')) {\n    console.error('package.json root must be an object');\n  }\n  throw err;\n}","preventionTips":["Keep package.json root as a JSON object.","Validate typeof parsed === 'object' after JSON.parse in CI.","Avoid tooling that writes scalar values into package.json."],"tags":["package-json","validation","json"],"analyzedSha":"06b6eae39da4ce0689fad64e2c48a6375a464208","analyzedAt":"2026-08-03T20:13:43.465Z","schemaVersion":2}