vuejs/core · error · Error
Failed to load TypeScript for resolving imported types.
Error message
Failed to load TypeScript for resolving imported types.
What it means
Thrown by the same lazy loadTS() wrapper in resolveType.ts when the TypeScript loader throws an error that is NOT the 'Cannot find module' shape. This is the fallback branch at resolveType.ts:907: TypeScript was found but failed to load (corrupt install, version incompatibility, CJS/ESM interop error, etc.).
Source
Thrown at packages/compiler-sfc/src/script/resolveType.ts:907
/**
* @private
*/
export function registerTS(_loadTS: () => typeof TS): void {
loadTS = () => {
try {
return _loadTS()
} catch (err: any) {
if (
typeof err.message === 'string' &&
err.message.includes('Cannot find module')
) {
throw new Error(
'Failed to load TypeScript, which is required for resolving imported types. ' +
'Please make sure "TypeScript" is installed as a project dependency.',
)
} else {
throw new Error(
'Failed to load TypeScript for resolving imported types.',
)
}
}
}
}
type FS = NonNullable<SFCScriptCompileOptions['fs']>
function resolveFS(ctx: TypeResolveContext): FS | undefined {
if (ctx.fs) {
return ctx.fs
}
if (!ts && loadTS) {
ts = loadTS()
}
const fs = ctx.options.fs || ts?.sys
if (!fs) {View on GitHub (pinned to a2b40db9a8)
Solutions
- Reinstall TypeScript cleanly: remove node_modules/typescript and the lockfile entry, then reinstall.
- Inspect the original error: temporarily wrap registerTS's loader to log err.message and err.stack before the rewrite fires.
- Pin a known-good TypeScript version compatible with your @vue/compiler-sfc release.
- If you pass a custom loader to registerTS, ensure it throws 'Cannot find module' semantics when missing and otherwise rethrows the real cause.
Example fix
// before — opaque failure
registerTS(() => require('typescript'))
// after — surface the real cause for diagnosis
registerTS(() => {
try {
return require('typescript')
} catch (e) {
console.error('[ts load failed]', e)
throw e
}
}) Defensive patterns
Strategy: try-catch
Validate before calling
// Probe-load TypeScript and surface the real failure.
function probeTS() {
try {
const ts = require('typescript')
if (!ts || typeof ts.version !== 'string') throw new Error('typescript loaded but invalid')
return true
} catch (e) {
console.error('[probeTS]', e)
return false
}
} Type guard
function typescriptLoadsCleanly(): boolean {
try {
const ts = require('typescript')
return !!ts && typeof ts.version === 'string'
} catch { return false }
} Try / catch
try {
registerTS(() => require('typescript'))
} catch (e) {
if (e instanceof Error && e.message.startsWith('Failed to load TypeScript')) {
// reinstall typescript, then retry once
await reinstallTypescript()
registerTS(() => require('typescript'))
} else { throw e }
} Prevention
- Pin a typescript version known to work with your @vue/compiler-sfc release.
- If you supply a custom loader to registerTS, preserve the 'Cannot find module' message shape so error 8 still fires distinctly.
- Reinstall typescript cleanly when its package looks corrupted (missing files, bad package.json).
When it happens
Trigger: TypeScript is present in node_modules but importing it throws for another reason — e.g. a broken/partial install, an ESM/CJS resolution error, an incompatible TS major version, a corrupted package.json. registerTS's loader throws something other than 'Cannot find module'.
Common situations: A botched `pnpm install` leaving typescript half-installed; an ESM-only environment failing to require() the CJS typescript entry; a peerDependency conflict resolving a forked/broken typescript; a loader closure that throws a custom error.
Related errors
- Failed to load TypeScript, which is required for resolving i
- [vue/compiler-sfc] globalTypeFiles requires fs access.
- [@vue/compiler-sfc] <script> and <script setup> must have th
- [@vue/compiler-sfc] SFC contains no <script> tags.
- [@vue/compiler-sfc] `modules` option is not supported in the
AI-assisted analysis of vuejs/core@a2b40db9a8 (2026-08-12).
Data as JSON: /api/errors/81119665ea754260.
Report an issue: GitHub.