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

  1. Install TypeScript as a project dependency: `pnpm add -D typescript` (or npm/yarn equivalent).
  2. If you call registerTS yourself, pass a loader that resolves typescript from the correct workspace path.
  3. Ensure your bundler does not externalize or tree-shake the typescript import for the compiler-sfc consumer.
  4. 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

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


AI-assisted analysis of vuejs/core@a2b40db9a8 (2026-08-12). Data as JSON: /api/errors/7505a1f0d0d61a84. Report an issue: GitHub.