vitest-dev/vitest · error · Error

Cannot parse ' ' because "module.stripTypeScriptTypes" is…

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

Native (Node-level) module mocking of TypeScript files requires `module.stripTypeScriptTypes`, which landed in Node.js 22.15. When the mocker receives a module whose format includes `typescript` and `module.stripTypeScriptTypes` is undefined, this error fires before any parsing attempt. The check is at nativeModuleMocker.ts:283-286.

Solutions

  1. Upgrade Node to 22.15 or newer (or any supported 24+).
  2. If you cannot upgrade Node, run the affected tests through the Vite-transformed mocker (disable native ESM mocking / ensure the module goes through Vite's TS plugin).
  3. Convert the mocked module's source to plain JavaScript so `stripTypeScriptTypes` is not needed.
  4. Pin the project's `engines.node` to `>=22.15` so the mismatch fails early at install.

Example fix

// before - .nvmrc
20.18.0

// after
22.15.0
Defensive patterns

Strategy: validation

Validate before calling

import module from 'node:module'; if (typeof module.stripTypeScriptTypes !== 'function' && isTsFile(filename)) throw new Error('upgrade Node to >=22.15 to mock TS modules natively')

Type guard

function supportsStripTypeScriptTypes(): boolean { return typeof (require('node:module') as any).stripTypeScriptTypes === 'function' }

Try / catch

null

Prevention

When it happens

Trigger: Calling `vi.mock` (automock, autospy, or manual) on a `.ts`/`.mts`/`.cts` module while running Vitest on Node.js < 22.15. The `transformCode` function detects `format.includes('typescript')` and finds no `stripTypeScriptTypes`.

Common situations: CI image pinned to Node 20 or 22.0-22.14; local dev on an older Node LTS; a Node version manager pointing at the wrong runtime; native ESM mocking enabled (which routes through the native mocker rather than the Vite-transformed path).

Related errors


AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11). Data as JSON: /api/errors/2fbc456f5988a172. Report an issue: GitHub.

Appendix: 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 1fa9837ec2)