{"record":{"id":"ddad2a821a36190e","repo":"vitest-dev/vitest","slug":"cannot-parse-the-module-format-of-url-because","errorCode":null,"errorMessage":"Cannot parse the module format of '${url}' because \"module.findPackageJSON\" is not available. Upgrade to Node 22.14 to use this feature. This is NOT a bug of Vitest.","messagePattern":"Cannot parse the module format of '(.+?)' because \"module\\.findPackageJSON\" is not available\\. Upgrade to Node 22\\.14 to use this feature\\. This is NOT a bug of Vitest\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/mocker/src/node/parsers.ts","lineNumber":138,"sourceCode":"    return []\n  }\n\n  return Array.from(new Set(exports))\n}\n\nexport function resolveModuleFormat(url: string, code: string): 'module' | 'commonjs' | undefined {\n  const ext = extname(url)\n\n  if (ext === '.cjs' || ext === '.cts') {\n    return 'commonjs'\n  }\n  else if (ext === '.mjs' || ext === '.mts') {\n    return 'module'\n  }\n  // https://nodejs.org/api/packages.html#syntax-detection\n  else if (ext === '.js' || ext === '.ts' || ext === '') {\n    if (!module.findPackageJSON) {\n      throw new Error(`Cannot parse the module format of '${url}' because \"module.findPackageJSON\" is not available. Upgrade to Node 22.14 to use this feature. This is NOT a bug of Vitest.`)\n    }\n    const pkgJsonPath = module.findPackageJSON(url)\n    const pkgJson = pkgJsonPath ? JSON.parse(readFileSync(pkgJsonPath, 'utf-8')) : {}\n    if (pkgJson?.type === 'module') {\n      return 'module'\n    }\n    else if (pkgJson?.type === 'commonjs') {\n      return 'commonjs'\n    }\n    else {\n      // Ambiguous input! Check if it has ESM syntax. Node.js is much smarter here,\n      // but we don't need to run the code, so we can be more relaxed\n      if (hasESM(filterOutComments(code))) {\n        return 'module'\n      }\n      else {\n        return 'commonjs'\n      }","sourceCodeStart":120,"sourceCodeEnd":156,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/mocker/src/node/parsers.ts#L120-L156","documentation":"Thrown by `resolveModuleFormat` in the node-side automock parser when a file has a `.js`/`.ts`/or extensionless path and `module.findPackageJSON` is unavailable on the running Node.js. To determine whether such a file is ESM or CommonJS, Vitest reads the nearest `package.json` `type` field via Node's native `module.findPackageJSON` (added in Node 22.14.0). Without it, the format is ambiguous and automock cannot proceed safely.","triggerScenarios":"Automocking a `.js` or extensionless module whose package type must be read from `package.json`, while running Node.js older than 22.14.0. Triggered during automock export analysis when `resolveModuleFormat` is called for the target file or any of its `export *` re-exports.","commonSituations":"Node 20 LTS or early Node 22 in CI/local; automocking a `.js` file in a `\"type\": \"module\"` or `\"type\": \"commonjs\"` package; automocking a dependency that re-exports from other `.js` files.","solutions":["Upgrade Node.js to 22.14.0 or newer.","Rename the target file to `.mjs`/`.mts` (forces ESM) or `.cjs`/`.cts` (forces CommonJS) so `resolveModuleFormat` short-circuits without `findPackageJSON`.","Provide an explicit `vi.mock(path, factory)` to bypass automock analysis."],"exampleFix":"// before — ambiguous .js module on Node < 22.14\nvi.mock('./mod.js')\n// after — explicit ESM extension, or upgrade Node to >= 22.14\n// rename mod.js -> mod.mjs, or:\nvi.mock('./mod.js', () => ({ fn: vi.fn() }))","handlingStrategy":"validation","validationCode":"// Check Node version before automocking ambiguous-extension modules.\nimport Module from 'node:module'\nfunction canFindPackageJSON(): boolean {\n  return typeof (Module as any).findPackageJSON === 'function'\n}\nif (!canFindPackageJSON()) {\n  // use explicit factories, or rename files to .mjs/.cjs to force a format\n}","typeGuard":"function nodeSupportsFindPackageJSON(): boolean {\n  return typeof (require('node:module') as any).findPackageJSON === 'function'\n}","tryCatchPattern":null,"preventionTips":["Pin Node.js to >= 22.14.0.","Use explicit .mjs/.cjs extensions to disambiguate module format without package.json lookup.","Provide explicit factories for ambiguous modules on older Node."],"tags":["automock","module-format","node-version","find-package-json"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}