Add TypeScript to an Existing JavaScript Project
Install TypeScript, generate a tsconfig.json, and convert files incrementally without breaking your build.
Requirements
- An existing Node.js project. Follow our guide →
Install TypeScript
$ npm install -D typescript @types/node@types/node provides type definitions for Node's built-in modules (fs, path, etc.) — without it, TypeScript treats them as untyped.
Generate a tsconfig.json
$ npx tsc --initThis scaffolds a tsconfig.json with sensible commented-out defaults. The settings you'll touch most:
`strict` — enables the full set of strict type-checks. `target` — which JS version to compile down to. `module` — the module system (esnext for bundlers, commonjs for plain Node). `outDir` — where compiled .js files go.
Migrate gradually
You don't need to convert everything at once. Set these two options so .ts and .js files coexist:
{ "compilerOptions": { "allowJs": true, "checkJs": false, "strict": false }}Rename one file at a time from .js to .ts, fix the type errors it surfaces, and move on to the next. Tighten strict to true once most of the codebase is converted.
Add a typecheck script
{ "scripts": { "typecheck": "tsc --noEmit" }}$ npm run typecheckSuccess
--noEmit type-checks without writing output files — useful as a CI step or pre-commit check.
Troubleshooting
TypeScript needs type information for every import, and the package either ships none or its `@types` package isn't installed.
$ npm install -D @types/xIf no @types package exists for it, declare a fallback module type in a .d.ts file:
declare module "x";