vitest-dev/vitest · error · Error

Cannot parse '${filename}' because "module.stripTypeScriptTy

Error message

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.

What it means

`transformCode` (`nativeModuleMocker.ts:285`) strips TypeScript types before parsing a module for mocking, using `module.stripTypeScriptTypes` — a Node API added in Node 22.15. On older Node, mocking a `.ts` (or `format` includes `typescript`) module is impossible, and this `Error` is thrown explicitly, clearly stating it is NOT a Vitest bug. Plain JS modules are unaffected.

Source

Thrown at packages/vitest/src/runtime/moduleRunner/nativeModuleMocker.ts:285

}

let __require: NodeJS.Require | undefined
function getBuiltinModule(moduleId: string) {
  __require ??= module.createRequire(import.meta.url)
  return __require(`${moduleId}?mock=actual`)
}

function genSourceMapUrl(map: SourceMap | string): string {
  if (typeof map !== 'string') {
    map = JSON.stringify(map)
  }
  return `data:application/json;base64,${Buffer.from(map).toString('base64')}`
}

function transformCode(code: string, format: string, filename: string) {
  if (format.includes('typescript')) {
    if (!module.stripTypeScriptTypes) {
      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.`)
    }
    return module.stripTypeScriptTypes(code)
  }
  return code
}

View on GitHub (pinned to d568f8ce37)

Solutions

  1. Upgrade Node to 22.15+ (or 24+) — Vitest's engines field lists supported versions.
  2. Provide a manual factory so the original TS source isn't parsed: `vi.mock('./mod.ts', () => ({...}))`.
  3. Mock only the compiled JS output instead of the `.ts` source.

Example fix

// before (Node < 22.15)
vi.mock('./util.ts') // automock — needs stripTypeScriptTypes

// after — manual factory avoids parsing the TS source
vi.mock('./util.ts', () => ({
  format: vi.fn(),
}))
Defensive patterns

Strategy: validation

Validate before calling

// Detect the Node API before mocking TS modules.
import module from 'node:module'
const supportsTsStripping = typeof module.stripTypeScriptTypes === 'function'
if (!supportsTsStripping) {
  throw new Error('Node < 22.15: use a manual vi.mock factory for TS modules')
}

Try / catch

try {
  vi.mock('./mod.ts')
} catch (e) {
  if (e instanceof Error && /stripTypeScriptTypes is not supported/.test(e.message)) {
    vi.mock('./mod.ts', () => ({ /* explicit stubs */ }))
  } else throw e
}

Prevention

When it happens

Trigger: `vi.mock('./mod.ts')` (automock or manual) running under Node < 22.15 (Node 20.x, early 22.x).

Common situations: Local dev or CI on Node 20 LTS; a Docker image pinned to an older Node; an Org policy delaying Node upgrades.

Related errors


AI-assisted analysis of vitest-dev/vitest@d568f8ce37 (2026-08-03). Data as JSON: /data/errors/2fbc456f5988a172.json. Report an issue: GitHub.