{"record":{"id":"81119665ea754260","repo":"vuejs/core","slug":"failed-to-load-typescript-for-resolving-imported-t","errorCode":null,"errorMessage":"Failed to load TypeScript for resolving imported types.","messagePattern":"Failed to load TypeScript for resolving imported types\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/compiler-sfc/src/script/resolveType.ts","lineNumber":907,"sourceCode":"\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  }\n  if (!ts && loadTS) {\n    ts = loadTS()\n  }\n  const fs = ctx.options.fs || ts?.sys\n  if (!fs) {","sourceCodeStart":889,"sourceCodeEnd":925,"githubUrl":"https://github.com/vuejs/core/blob/a2b40db9a83b36ed9da3a16403cf8f040262d73f/packages/compiler-sfc/src/script/resolveType.ts#L889-L925","documentation":"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.).","triggerScenarios":"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'.","commonSituations":"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.","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."],"exampleFix":"// before — opaque failure\nregisterTS(() => require('typescript'))\n\n// after — surface the real cause for diagnosis\nregisterTS(() => {\n  try {\n    return require('typescript')\n  } catch (e) {\n    console.error('[ts load failed]', e)\n    throw e\n  }\n})","handlingStrategy":"try-catch","validationCode":"// Probe-load TypeScript and surface the real failure.\nfunction probeTS() {\n  try {\n    const ts = require('typescript')\n    if (!ts || typeof ts.version !== 'string') throw new Error('typescript loaded but invalid')\n    return true\n  } catch (e) {\n    console.error('[probeTS]', e)\n    return false\n  }\n}","typeGuard":"function typescriptLoadsCleanly(): boolean {\n  try {\n    const ts = require('typescript')\n    return !!ts && typeof ts.version === 'string'\n  } catch { return false }\n}","tryCatchPattern":"try {\n  registerTS(() => require('typescript'))\n} catch (e) {\n  if (e instanceof Error && e.message.startsWith('Failed to load TypeScript')) {\n    // reinstall typescript, then retry once\n    await reinstallTypescript()\n    registerTS(() => require('typescript'))\n  } else { throw e }\n}","preventionTips":["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)."],"tags":["vue","compiler-sfc","typescript","resolvetype","dependencies","debugging"],"backgroundTag":null,"analyzedSha":"a2b40db9a83b36ed9da3a16403cf8f040262d73f","analyzedAt":"2026-08-12T14:21:14.989Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}