{"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/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/mocker/src/node/parsers.ts#L120-L156","documentation":"To classify a `.js`/`.ts`/extensionless file as ESM or CJS, the mocker reads the nearest `package.json` `\"type\"` field through Node's `module.findPackageJSON` (added in Node 22.14). When that function is missing, the module format cannot be determined safely, so `resolveModuleFormat` throws instead of guessing wrong and producing an invalid mock.","triggerScenarios":"Mocking a `.js` dependency whose ESM/CJS nature must be resolved from `package.json`, on Node older than 22.14, hitting the `!module.findPackageJSON` guard at parsers.ts:137.","commonSituations":"Node 20 LTS in CI; mixed ESM/CJS monorepo where the same specifier resolves differently per package; Docker images built on older Node; upgrading Vitest but not the runtime.","solutions":["Upgrade Node.js to 22.14 or newer.","Give the target an explicit extension: rename to `.mjs` or `.cjs` so `resolveModuleFormat` short-circuits before needing `findPackageJSON`.","Provide an explicit factory via `vi.mock(specifier, () => ({ ... }))` to bypass format detection."],"exampleFix":"// before (ambiguous .js, Node < 22.14)\nvi.mock('./legacy.js')\n\n// after: explicit extension avoids package.json lookup\nvi.mock('./legacy.cjs')","handlingStrategy":"validation","validationCode":"import module from 'node:module'\n\nconst canFindPkg = typeof module.findPackageJSON === 'function'\nif (!canFindPkg) {\n  console.warn(\n    `vi.mock format detection needs Node >= 22.14 (module.findPackageJSON); current is ${process.version}.`,\n  )\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pin Node >= 22.14 in CI and Docker base images.","Prefer explicit `.mjs`/`.cjs` extensions for files you intend to mock.","Supply explicit factories when format detection is unreliable."],"tags":["node-version","mocking","esm","cjs"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}