{"record":{"id":"dc3d00f7f88a0bad","repo":"remix-run/remix","slug":"invalid-package-json-at-packagejsonpath-expect","errorCode":null,"errorMessage":"Invalid package.json at ${packageJsonPath}. Expected an object.","messagePattern":"Invalid package\\.json at (.+?)\\. Expected an object\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/node-tsx/src/lib/package-type.ts","lineNumber":46,"sourceCode":"    let packageJsonPath = path.join(directory, 'package.json')\n    if (fs.existsSync(packageJsonPath)) {\n      return packageJsonPath\n    }\n\n    let parentDirectory = path.dirname(directory)\n    if (parentDirectory === directory) {\n      return null\n    }\n\n    directory = parentDirectory\n  }\n}\n\nfunction readPackageType(packageJsonPath: string): ModuleFormat {\n  try {\n    let packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))\n    if (!isRecord(packageJson)) {\n      throw new Error(`Invalid package.json at ${packageJsonPath}. Expected an object.`)\n    }\n\n    return packageJson.type === 'module' ? 'module' : 'commonjs'\n  } catch (error) {\n    let nodeError = error as NodeJS.ErrnoException\n    if (nodeError.code === 'ENOENT' || nodeError.code === 'ENOTDIR') {\n      return 'commonjs'\n    }\n\n    throw error\n  }\n}\n\nfunction hasModuleSyntax(source: string): boolean {\n  if (/\\bimport\\.meta\\b/.test(source)) {\n    return true\n  }\n","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/remix-run/remix/blob/9696913134be3a4423513d2775f7b31d6917c049/packages/node-tsx/src/lib/package-type.ts#L28-L64","documentation":"The node-tsx loader reads a package.json file while resolving the module format for a file, but the parsed content is not a JSON object (e.g. it is an array, string, number, or null). Since module format detection requires reading the `type` field from an object, remix/node-tsx throws rather than guessing. This almost always means a malformed or hand-edited package.json on disk.","triggerScenarios":"Running code through remix/node-tsx (or a Remix dev/SSR server using it) where a directory in the path chain contains a package.json whose top-level JSON value is not an object, such as `[]`, `\"text\"`, `null`, or `123`. ENOENT is handled separately, so the file exists but is structurally invalid.","commonSituations":"Truncated or corrupted package.json from a killed install, a package.json containing a bare JSON string or comment-style syntax that accidentally parses to a non-object, a fixture/test file written with invalid content, or a pnpm workspace symlink pointing at a broken package.","solutions":["Inspect the package.json at the path in the error message and confirm it parses to an object (`node -e \"console.log(typeof require('./package.json'))\"`)","Fix or restore the file — usually `git checkout -- <path>/package.json` or reinstall dependencies","If it is a fixture used for testing module resolution, rewrite it as `{}`","Validate all package.json files in the project: `npx pnpm install` or a JSON linter"],"exampleFix":"// before: contents of ./broken/package.json\n\"just a string\"\n\n// after\n{\n  \"type\": \"module\"\n}","handlingStrategy":"validation","validationCode":"import fs from 'node:fs'\n\nfunction isValidPackageJson(path) {\n  try {\n    return typeof JSON.parse(fs.readFileSync(path, 'utf8')) === 'object' && JSON.parse(fs.readFileSync(path, 'utf8')) !== null && !Array.isArray(JSON.parse(fs.readFileSync(path, 'utf8')))\n  } catch { return false }\n}","typeGuard":"function isRecord(value: unknown): value is Record<string, unknown> {\n  return typeof value === 'object' && value !== null && !Array.isArray(value)\n}","tryCatchPattern":"Not applicable — fix the file on disk rather than catching.","preventionTips":["Keep package.json files under version control and restore via git rather than hand-editing","Run `pnpm install` after interruptions to repair corrupted files","Lint fixtures that write package.json for tests"],"tags":["node-tsx","package-json","module-resolution"],"backgroundTag":"malformed-package-json","analyzedSha":"9696913134be3a4423513d2775f7b31d6917c049","analyzedAt":"2026-08-27T19:55:01.024Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}