ts-node-dev for TypeScript: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

ts-node-dev for TypeScript: Complete Guide with Examples

\n\n

Welcome 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\n

1. What It Is and Why You Need It

\n\n

ts-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\n

Why 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
\n\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.

\n\n

2. Installation

\n\n

Installing 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\n

After installation, verify everything works:

\n
npx ts-node-dev --version
\n\n

If you see a version number (e.g., 2.0.0), the installation was successful.

\n\n

3. Quick Start — Minimal Working Example

\n\n

Let's create a simple TypeScript application and run it with ts-node-dev.

\n\n

Step 1: Initialize the project

\n
mkdir my-ts-app\ncd my-ts-app\nnpm init -y\nnpm install --save-dev typescript ts-node-dev @types/node\nnpx tsc --init
\n\n

Step 2: Create the file src/index.ts

\n
// 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\n

Step 3: Run with ts-node-dev

\n
npx ts-node-dev --respawn src/index.ts
\n\n

You 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\n

Now 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.

\n\n

4. Main Methods/Functions/Classes

\n\n

ts-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\n

4.1 --respawn

\n

Signature: ts-node-dev --respawn [file]

\n

What it does: Automatically restarts the process when any files in the project change. This is the main option for hot reloading.

\n

Working example:

Recommendations