{"id":"e6a486ab6dc7cd8d","repo":"vitest-dev/vitest","slug":"cannot-parse-filename-because-module-stripty","errorCode":null,"errorMessage":"Cannot parse '${filename}' because \"module.stripTypeScriptTypes\" is not supported. Module mocking requires Node.js 22.15 or higher. This is NOT a bug of Vitest.","messagePattern":"Cannot parse '(.+?)' because \"module\\.stripTypeScriptTypes\" is not supported\\. Module mocking requires Node\\.js 22\\.15 or higher\\. This is NOT a bug of Vitest\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/mocker/src/node/parsers.ts","lineNumber":26,"sourceCode":"\nexport async function initSyntaxLexers(): Promise<void> {\n  await Promise.all([\n    initCjsLexer(),\n    initModuleLexer,\n  ])\n}\n\nconst isTransform = process.execArgv.includes('--experimental-transform-types')\n  || process.env.NODE_OPTIONS?.includes('--experimental-transform-types')\n\nexport function transformCode(code: string, filename: string): string {\n  const ext = extname(filename.split('?')[0])\n  const isTs = ext === '.ts' || ext === '.cts' || ext === '.mts'\n  if (!isTs) {\n    return code\n  }\n  if (!module.stripTypeScriptTypes) {\n    throw new Error(`Cannot parse '${filename}' because \"module.stripTypeScriptTypes\" is not supported. Module mocking requires Node.js 22.15 or higher. This is NOT a bug of Vitest.`)\n  }\n  return module.stripTypeScriptTypes(code, { mode: isTransform ? 'transform' : 'strip' })\n}\n\nconst cachedFileExports = new Map<string, string[]>()\n\nexport function collectModuleExports(\n  filename: string,\n  code: string,\n  format: 'module' | 'commonjs',\n  exports: string[] = [],\n): string[] {\n  if (format === 'module') {\n    const [imports_, exports_] = parseModuleSyntax(code, filename)\n    const fileExports = [...exports_.map(p => p.n)]\n    imports_.forEach(({ ss: start, se: end, n: name }) => {\n      const substring = code.substring(start, end).replace(/ +/g, ' ')\n      if (name && substring.startsWith('export *') && !substring.startsWith('export * as')) {","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/vitest-dev/vitest/blob/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/mocker/src/node/parsers.ts#L8-L44","documentation":"Vitest's mocker must statically enumerate a mocked module's exports, which for TypeScript sources requires stripping types via Node's `module.stripTypeScriptTypes` (added in Node 22.15). On an older Node runtime that API is `undefined`, so `transformCode` in parsers.ts refuses to guess and throws rather than silently producing a broken automock.","triggerScenarios":"Calling `vi.mock('./mod.ts')` (or letting Vitest automock a `.ts`/`.cts`/`.mts` dependency) while running on Node older than 22.15, where the `!module.stripTypeScriptTypes` guard at parsers.ts:25 is true.","commonSituations":"CI pinned to Node 20 LTS; local dev on Node 18/20; Docker base image lagging; upgrading Vitest without bumping the Node engines; monorepos whose root `engines.node` still allows v20.","solutions":["Upgrade Node.js to 22.15 or newer (verify with `node -e \"console.log(typeof require('module').stripTypeScriptTypes)\"`).","If you cannot upgrade, supply a full factory so Vitest skips export analysis: `vi.mock('./mod.ts', () => ({ default: stub }))`.","Mock the compiled `.js`/`.mjs` output instead of the `.ts` source so type stripping is unnecessary."],"exampleFix":"// before\nvi.mock('./config.ts')\n\n// after (Node < 22.15 workaround)\nvi.mock('./config.ts', () => ({\n  default: { port: 3000, host: 'localhost' },\n}))","handlingStrategy":"validation","validationCode":"import module from 'node:module'\n\nconst canStripTs = typeof module.stripTypeScriptTypes === 'function'\nif (!canStripTs) {\n  console.warn(\n    `vi.mock of TypeScript modules needs Node >= 22.15; current is ${process.version}. ` +\n    `Provide an explicit factory or upgrade Node.`,\n  )\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pin `engines.node` to `>=22.15` in package.json and enforce it in CI.","When targeting Node < 22.15, always pass a full factory to `vi.mock` for TS modules.","Add a smoke test that mocks one TS module so version regressions fail fast in CI."],"tags":["node-version","mocking","typescript","automock"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}