ts-node-dev for TypeScript: Complete Guide with Examples
\n\nWelcome to PythonLib! In this article, we will thoroughly examine the ts-node-dev library — an indispensable tool for modern TypeScript development. If you write in TypeScript and are tired of manually recompiling code after every change, this article is for you.
\n\n1. What It Is and Why You Need It
\n\nts-node-dev is a utility for TypeScript that provides hot reload for your application. It automatically restarts the Node.js process when TypeScript source files change. This significantly speeds up development, as you don't need to manually stop and restart the server or script.
\n\nWhy is this needed? During development, you constantly make changes to the code. Without automatic reloading, you have to:
\n- \n
- Stop the current process (Ctrl+C) \n
- Restart it (npm start or ts-node) \n
- Wait for compilation and startup \n
ts-node-dev solves this problem. It monitors changes to project files and instantly restarts the application. Unlike nodemon (which works with JavaScript), ts-node-dev understands TypeScript and uses ts-node under the hood for on-the-fly compilation. This makes it an ideal choice for developing servers, CLI tools, and any Node.js applications in TypeScript.
2. Installation
\n\nInstalling ts-node-dev is done via npm (Node Package Manager). It is recommended to install it as a dev dependency, as it is only needed during development.
\n\n# Global installation (not recommended)\nnpm install -g ts-node-dev\n\n# Installation as a dev dependency in the project (recommended)\nnpm install --save-dev ts-node-dev\n\n# Or via yarn\nyarn add --dev ts-node-dev\n\n# Or via pnpm\npnpm add -D ts-node-dev\n\nAfter installation, verify everything works:
\nnpx ts-node-dev --version\n\nIf you see a version number (e.g., 2.0.0), the installation was successful.
\n\n3. Quick Start — Minimal Working Example
\n\nLet's create a simple TypeScript application and run it with ts-node-dev.
\n\nStep 1: Initialize the project
\nmkdir my-ts-app\ncd my-ts-app\nnpm init -y\nnpm install --save-dev typescript ts-node-dev @types/node\nnpx tsc --init\n\nStep 2: Create the file src/index.ts
// src/index.ts\nconst greet = (name: string): string => {\n return `Hello, ${name}!`;\n};\n\nconsole.log(greet('PythonLib'));\nconsole.log('Server is running...');\n\n// Export something for example\nexport { greet };\n\nStep 3: Run with ts-node-dev
\nnpx ts-node-dev --respawn src/index.ts\n\nYou should see:
\n[INFO] 15:30:00 ts-node-dev ver. 2.0.0 (using ts-node ver. 10.9.1, typescript ver. 5.3.3)\nHello, PythonLib!\nServer is running...\n\nNow change the greeting text in src/index.ts and save the file. You will see that the process automatically restarts and outputs the new result.
4. Main Methods/Functions/Classes
\n\nts-node-dev is a CLI tool, not a library with an API. However, it has many options (flags) that can be considered as configuration "methods". Let's look at 8 key options.
\n\n4.1 --respawn
\nSignature: ts-node-dev --respawn [file]
What it does: Automatically restarts the process when any files in the project change. This is the main option for hot reloading.
\nWorking example:
Also in library
Recommendations