How can I run tsc –watch and nodemon together using npm scripts?

Miley
Updated 5 days ago in

Hi everyone,

I’m learning Node.js with TypeScript and I’m trying to improve my development workflow. Right now, I compile my TypeScript files and then manually restart the server whenever I make changes. I was wondering if there’s a way to use npm scripts to run both the TypeScript compiler in watch mode and nodemon at the same time.

My current package.json scripts look like this:

 
{
  "scripts": {
    "build": "tsc",
    "start": "node dist/index.js",
    "dev": "tsc --watch"
  }
}
 

I’d like to automatically recompile my TypeScript files and have nodemon restart the server whenever the compiled JavaScript changes. Is there a recommended way to do this using npm scripts, or should I use another tool like concurrently or npm-run-all?

I’m still learning, so I’d appreciate a beginner-friendly explanation. Thanks!

  • 1
  • 40
  • 5 days ago
 
5 days ago

Yes, you can run tsc --watch and nodemon together using npm scripts. This is a common setup for TypeScript projects because it allows the TypeScript compiler to automatically rebuild your code while nodemon restarts the Node.js application whenever the compiled JavaScript changes.

The easiest way to achieve this is by using a utility such as concurrently or npm-run-all, which lets multiple scripts run in parallel from a single command.

A typical workflow looks like this:

  • tsc --watch continuously compiles TypeScript files into JavaScript.
  • nodemon watches the compiled output directory (usually dist) and restarts the server whenever a new build is generated.
  • Running both together gives you a smooth development experience without manually rebuilding or restarting the application.

A few best practices:

  • Configure nodemon to watch the compiled JavaScript output rather than the TypeScript source files.
  • Keep your compiled files in a separate directory (such as dist) to avoid unnecessary restarts.
  • Enable source maps in your TypeScript configuration if you need easier debugging.
  • Exclude folders like node_modules and build artifacts that don’t need to trigger restarts.

If you’re using a newer development setup, you might also consider tools like tsx, ts-node-dev, or nodemon with ts-node, which can execute TypeScript directly and often eliminate the need for a separate compilation step during development.

For most projects, however, running tsc --watch alongside nodemon remains a reliable and widely used workflow because it closely mirrors how the application is built and run in production.

 
  • Liked by
Reply
Cancel
Loading more replies