vuejs/core · error · Error
Failed to load TypeScript, which is required for resolving i
Error message
Failed to load TypeScript, which is required for resolving imported types. Please make sure "TypeScript" is installed as a project dependency.
What it means
Thrown by the lazy loadTS() wrapper in @vue/compiler-sfc's type resolver when the underlying TS loader throws an error whose message contains 'Cannot find module'. Type resolution for imported types needs the TypeScript compiler API; if TS is not installed the loader cannot proceed. resolveType.ts:902 rethrows with an actionable install hint.
Source
Thrown at packages/compiler-sfc/src/script/resolveType.ts:902
}
}
let ts: typeof TS | undefined
let loadTS: (() => typeof TS) | undefined
/**
* @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
}View on GitHub (pinned to a2b40db9a8)
Solutions
- Install TypeScript as a project dependency: `pnpm add -D typescript` (or npm/yarn equivalent).
- If you call registerTS yourself, pass a loader that resolves typescript from the correct workspace path.
- Ensure your bundler does not externalize or tree-shake the typescript import for the compiler-sfc consumer.
- If type resolution is not needed, avoid the code paths that trigger it (e.g. type-based defineProps with imported types).
Example fix
// before — typescript missing
// (error at compile time)
// after
pnpm add -D typescript
// or register explicitly
import { registerTS } from '@vue/compiler-sfc'
registerTS(() => require('typescript')) Defensive patterns
Strategy: validation
Validate before calling
// Detect a resolvable typescript before triggering type resolution.
function canResolveTypescript() {
try {
require.resolve('typescript')
return true
} catch {
return false
}
}
if (!canResolveTypescript()) {
throw new Error('Install typescript: pnpm add -D typescript')
} Type guard
function typescriptIsInstalled(): boolean {
try { require.resolve('typescript'); return true } catch { return false }
} Try / catch
try {
result = compileScript(sfc, optsWithGlobalTypes)
} catch (e) {
if (e instanceof Error && e.message.includes('Please make sure "TypeScript" is installed')) {
// install or register TS, then retry
registerTS(() => require('typescript'))
result = compileScript(sfc, optsWithGlobalTypes)
} else { throw e }
} Prevention
- List typescript as a devDependency in any package that uses type-aware SFC compilation.
- Call registerTS(() => require('typescript')) at process start in your build tool.
- In monorepos, ensure typescript is hoisted/visible to the compiling package.
When it happens
Trigger: Calling any SFC compile path that resolves imported types (e.g. defineProps with type-only imports, generic components) when typescript is not resolvable from the project. registerTS was given a loader whose require('typescript') fails with 'Cannot find module'.
Common situations: A monorepo workspace that hoists typescript but the consuming package cannot resolve it; using @vue/compiler-sfc in a project that never installed typescript; a bundler externalization that drops typescript from node_modules.
Related errors
- [vue/compiler-sfc] globalTypeFiles requires fs access.
- Failed to load TypeScript for resolving imported types.
- [@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/7505a1f0d0d61a84.
Report an issue: GitHub.