Nodemon's exec Flag: Automating TypeScript Restarts Without a Manual Build Step
Nodemon's exec flag wraps your Node process to run ts-node, giving TypeScript developers automatic restarts without a separate build step—if you account for startup overhead and error visibility.
04 May 2026, 11:48 UTC

If you build Node.js services in TypeScript, the edit-save-restart loop is familiar: change a .ts file, run a transpiler, restart the process. Nodemon solves this for plain JavaScript by watching files and restarting node, but it does not handle TypeScript syntax on its own. The exec option closes that gap by replacing the default node command with a custom command such as ts-node.
How the exec wrapper works
Nodemon is a process supervisor, not a compiler. It spawns a child process—a separate program launched by nodemon—and watches your files. When a watched file changes, nodemon kills that child and starts a new one. The exec flag changes what command is used for the replacement child.
For a TypeScript project, you can point exec at ts-node src/index.ts. ts-node reads TypeScript directly, transpiles it in memory, and runs the result. Because the transpilation happens inside the child process, you never run tsc manually during development, and stack traces generally map back to your original TypeScript lines.
Persist the setting in nodemon.json at the project root, or under a nodemonConfig key in package.json. A minimal nodemon.json looks like this:
{
"watch": ["src"],
"ext": "ts",
"ignore": ["dist", "node_modules"],
"exec": "ts-node src/index.ts"
}
The watch array limits monitoring to source files. The ext field tells nodemon which extensions trigger restarts; without it, nodemon may miss .ts edits. The ignore array prevents restart loops when build tools or installers touch files.
A worked example
Run these commands in your terminal from an empty project directory:
mkdir nodemon-ts-demo
cd nodemon-ts-demo
npm init -y
npm install --save-dev typescript ts-node nodemon @types/node
npx tsc --init
No elevated permissions are needed. Create src/index.ts:
const greeting: string = "hello from typescript";
console.log(greeting);
Add the nodemon.json above, then start the watcher:
npx nodemon
Expected behavior: nodemon starts, your app prints the greeting, and saving a change to src/index.ts triggers a restart with updated output—no manual tsc step. To confirm the old child is gone, run ps aux | grep node on macOS or Linux, or check Task Manager on Windows, and look for a new process ID after each save.
Trade-offs to plan for
Startup overhead. ts-node transpiles on every launch. Small scripts restart quickly, but large codebases can feel sluggish compared with plain JavaScript. Narrow the watch scope if latency becomes annoying.
Error visibility. Nodemon restarts on every watched change, even when the exec command fails. Introduce a type error and save, and ts-node will print a compilation error, but nodemon will still spawn a new child on the next save. The error is not swallowed, yet the watcher does not pause—you must read stdout and stderr.
File-watching pressure. Large repositories can drive CPU usage up when too many files are watched. The ignore patterns above keep nodemon from reacting to node_modules, build output, or version-control metadata.
Checking the result
To validate the setup, add a deliberate type error such as const broken: number = "text"; to src/index.ts and save. Nodemon should restart, and ts-node should surface the error in the console. Remove the line, save again, and the app should recover automatically. This workflow is for development only; production services should run compiled JavaScript.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.