How to use TypeScript with Node.js

· Category: TypeScript

Short answer

Install TypeScript and Node type definitions, create tsconfig.json with Node settings, and run code with ts-node or compile to JS. For file system operations, see python read write files. For asynchronous programming, see python asyncio.

Steps

  1. Install: npm install -D typescript @types/node ts-node
  2. Create tsconfig.json with "module": "commonjs" and "target": "es2020"
  3. Set "outDir": "./dist" and "rootDir": "./src"
  4. Run directly: npx ts-node src/index.ts
  5. Build and run: npx tsc && node dist/index.js

Tips

  • Use tsx for faster TypeScript execution in development
  • Configure path aliases in tsconfig.json for cleaner imports
  • For handling errors gracefully, see python error handling try except