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
Thrown by `transformCode` in the node-side automock parser when the target file has a TypeScript extension (`.ts`/`.cts`/`.mts`) but `module.stripTypeScriptTypes` is not available on the running Node.js. Automocking a TypeScript module requires stripping its types before parsing, and Vitest delegates this to Node's native `module.stripTypeScriptTypes` API, which was added in Node.js 22.15.0. The message is explicit that this is a Node version limitation, not a Vitest bug.
Solutions
- Upgrade Node.js to 22.15.0 or newer (the error message states this requirement).
- Provide an explicit factory: `vi.mock(path, factory)` to bypass automock's TS-stripping path.
- Pin your CI/Docker base image to `node:22.15` or later and update `engines.node` accordingly.
Example fix
// before — automock a TS module on Node < 22.15
vi.mock('./ts-mod')
// after — explicit factory avoids transformCode
vi.mock('./ts-mod', () => ({ fn: vi.fn() }))
// or: upgrade Node to >= 22.15 Defensive patterns
Strategy: validation
Validate before calling
// Check Node version before automocking TS modules.
import Module from 'node:module'
function canStripTypeScriptTypes(): boolean {
return typeof (Module as any).stripTypeScriptTypes === 'function'
}
if (!canStripTypeScriptTypes()) {
// provide explicit factories, or fail fast with an actionable message
} Type guard
function nodeSupportsStripTypeScriptTypes(): boolean {
return typeof (require('node:module') as any).stripTypeScriptTypes === 'function'
} Prevention
- Pin Node.js to >= 22.15.0 in CI, Docker, and local toolchain.
- Set engines.node in package.json to ^22.15.0 || >=22.15.0.
- Provide explicit vi.mock factories for TS modules on older Node as a stopgap.
When it happens
Trigger: Automocking (`vi.mock(path)` without a factory) a `.ts`/`.mts`/`.cts` module that contains TypeScript type annotations, while running on Node.js older than 22.15.0. `transformCode` is also invoked transitively when resolving `export *` re-exports during automock analysis.
Common situations: CI or local environment pinned to Node 20 LTS or an early Node 22; Docker images using an older `node:22` tag; `engines` in package.json not enforcing the minimum; automocking a TS dependency that itself re-exports from other TS files.
Related errors
- Cannot parse ' ' because "module.stripTypeScriptTypes" is…
- Cannot parse the module format of
- Cannot parse ' ' because "module.stripTypeScriptTypes" is…
- AssignmentPattern is not supported. Please open a new bug…
- automocking files with `export *` is not supported because…
AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11).
Data as JSON: /api/errors/e6a486ab6dc7cd8d.
Report an issue: GitHub.
Appendix: source
Thrown at packages/mocker/src/node/parsers.ts:26
export async function initSyntaxLexers(): Promise<void> {
await Promise.all([
initCjsLexer(),
initModuleLexer,
])
}
const isTransform = process.execArgv.includes('--experimental-transform-types')
|| process.env.NODE_OPTIONS?.includes('--experimental-transform-types')
export function transformCode(code: string, filename: string): string {
const ext = extname(filename.split('?')[0])
const isTs = ext === '.ts' || ext === '.cts' || ext === '.mts'
if (!isTs) {
return code
}
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, { mode: isTransform ? 'transform' : 'strip' })
}
const cachedFileExports = new Map<string, string[]>()
export function collectModuleExports(
filename: string,
code: string,
format: 'module' | 'commonjs',
exports: string[] = [],
): string[] {
if (format === 'module') {
const [imports_, exports_] = parseModuleSyntax(code, filename)
const fileExports = [...exports_.map(p => p.n)]
imports_.forEach(({ ss: start, se: end, n: name }) => {
const substring = code.substring(start, end).replace(/ +/g, ' ')
if (name && substring.startsWith('export *') && !substring.startsWith('export * as')) {View on GitHub (pinned to 1fa9837ec2)