{"record":{"id":"7505a1f0d0d61a84","repo":"vuejs/core","slug":"failed-to-load-typescript-which-is-required-for-r","errorCode":null,"errorMessage":"Failed to load TypeScript, which is required for resolving imported types. Please make sure \"TypeScript\" is installed as a project dependency.","messagePattern":"Failed to load TypeScript, which is required for resolving imported types\\. Please make sure \"TypeScript\" is installed as a project dependency\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/compiler-sfc/src/script/resolveType.ts","lineNumber":902,"sourceCode":"  }\n}\n\nlet ts: typeof TS | undefined\nlet loadTS: (() => typeof TS) | undefined\n\n/**\n * @private\n */\nexport function registerTS(_loadTS: () => typeof TS): void {\n  loadTS = () => {\n    try {\n      return _loadTS()\n    } catch (err: any) {\n      if (\n        typeof err.message === 'string' &&\n        err.message.includes('Cannot find module')\n      ) {\n        throw new Error(\n          'Failed to load TypeScript, which is required for resolving imported types. ' +\n            'Please make sure \"TypeScript\" is installed as a project dependency.',\n        )\n      } else {\n        throw new Error(\n          'Failed to load TypeScript for resolving imported types.',\n        )\n      }\n    }\n  }\n}\n\ntype FS = NonNullable<SFCScriptCompileOptions['fs']>\n\nfunction resolveFS(ctx: TypeResolveContext): FS | undefined {\n  if (ctx.fs) {\n    return ctx.fs\n  }","sourceCodeStart":884,"sourceCodeEnd":920,"githubUrl":"https://github.com/vuejs/core/blob/a2b40db9a83b36ed9da3a16403cf8f040262d73f/packages/compiler-sfc/src/script/resolveType.ts#L884-L920","documentation":"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.","triggerScenarios":"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'.","commonSituations":"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.","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)."],"exampleFix":"// before — typescript missing\n// (error at compile time)\n\n// after\npnpm add -D typescript\n// or register explicitly\nimport { registerTS } from '@vue/compiler-sfc'\nregisterTS(() => require('typescript'))","handlingStrategy":"validation","validationCode":"// Detect a resolvable typescript before triggering type resolution.\nfunction canResolveTypescript() {\n  try {\n    require.resolve('typescript')\n    return true\n  } catch {\n    return false\n  }\n}\nif (!canResolveTypescript()) {\n  throw new Error('Install typescript: pnpm add -D typescript')\n}","typeGuard":"function typescriptIsInstalled(): boolean {\n  try { require.resolve('typescript'); return true } catch { return false }\n}","tryCatchPattern":"try {\n  result = compileScript(sfc, optsWithGlobalTypes)\n} catch (e) {\n  if (e instanceof Error && e.message.includes('Please make sure \"TypeScript\" is installed')) {\n    // install or register TS, then retry\n    registerTS(() => require('typescript'))\n    result = compileScript(sfc, optsWithGlobalTypes)\n  } else { throw e }\n}","preventionTips":["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."],"tags":["vue","compiler-sfc","typescript","resolvetype","dependencies","configuration"],"backgroundTag":null,"analyzedSha":"a2b40db9a83b36ed9da3a16403cf8f040262d73f","analyzedAt":"2026-08-12T14:21:14.989Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}