sveltejs/kit · error · Error
You need to install TypeScript if you want to transpile Type
Error message
You need to install TypeScript if you want to transpile TypeScript files and/or generate type definitions
What it means
`try_load_ts` dynamically imports the `typescript` package and rethrows any failure as a clear error: @sveltejs/package needs TypeScript itself to transpile TS files or generate .d.ts. TypeScript is an optional peer, so it is not auto-installed with the packager.
Source
Thrown at packages/package/src/typescript.js:217
const path = pattern.text.startsWith('!') ? pattern.text.slice(1) : pattern.text;
if (!(path.startsWith('./') || path.startsWith('../')) || !path.endsWith('.ts')) {
return pattern;
}
const rewritten = pattern.text.slice(0, -3) + '.js';
const literal = ts.isStringLiteral(pattern)
? ts.factory.createStringLiteral(rewritten, pattern.getText().startsWith("'"))
: ts.factory.createNoSubstitutionTemplateLiteral(rewritten);
return ts.setTextRange(literal, pattern);
}
async function try_load_ts() {
try {
return (await import('typescript')).default;
} catch {
throw new Error(
'You need to install TypeScript if you want to transpile TypeScript files and/or generate type definitions'
);
}
}
/**
* @param {string | undefined} tsconfig
* @param {string} filename
* @param {Map<string, import('typescript').CompilerOptions>} cache
* @param {typeof import('typescript')} [ts]
*/
export async function load_tsconfig(tsconfig, filename, cache, ts) {
if (!ts) {
ts = await try_load_ts();
}
let config_filename;
/** @type {string[]} */View on GitHub (pinned to 03f1687fe6)
Solutions
- Install TypeScript: `npm i -D typescript`.
- In pnpm workspaces, declare typescript in the package's devDependencies so it links locally.
- Verify `node_modules/typescript` exists and imports cleanly; reinstall node_modules if corrupted.
Example fix
// before
"devDependencies": { "@sveltejs/package": "^2.0.0" }
// after
"devDependencies": { "@sveltejs/package": "^2.0.0", "typescript": "^5.0.0" } Defensive patterns
Strategy: validation
Validate before calling
try { await import('typescript'); } catch { throw new Error('add typescript to devDependencies'); } Try / catch
try {
await run(['svelte-package']);
} catch (e) {
if (String(e.message).includes('You need to install TypeScript')) {
throw new Error('Run: npm i -D typescript');
}
throw e;
} Prevention
- Include typescript in devDependencies whenever the project contains .ts files
- Run install after cloning before running build scripts
- Keep typescript version within the range supported by @sveltejs/package
When it happens
Trigger: Running `svelte-package` on a project that includes `.ts`/`.svelte` files needing transpilation or `types` generation while `typescript` is absent from node_modules (dynamic `import('typescript')` rejects).
Common situations: Library repo without typescript in devDependencies; pnpm strict installs where typescript wasn't declared; broken/incompatible typescript install causing the import to fail.
Related errors
- Could not resolve peer dependency "${name}" relative to your
- ${keypath} should be a function, if specified
- Could not resolve peer dependency "${name}" relative to your
- Could not find valid "${subpackage}" export in ${name}/packa
- Failed to locate provided tsconfig or jsconfig
AI-assisted analysis of sveltejs/kit@03f1687fe6 (2026-09-02).
Data as JSON: /api/errors/0ce3a5ef2707c828.
Report an issue: GitHub.