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

A common approach is to run both processes concurrently—one process watches and recompiles your TypeScript files, while the other watches the compiled JavaScript files and restarts your application when changes are detected.

A popular way to do this is by using the concurrently package:

{
  "scripts": {
    "build:watch": "tsc --watch",
    "serve": "nodemon dist/index.js",
    "dev": "concurrently \"npm run build:watch\" \"npm run serve\""
  }
}

In this setup:

  • tsc --watch recompiles your TypeScript code whenever a source file changes.
  • nodemon monitors the compiled output (typically the dist folder) and automatically restarts the application after a successful build.
  • concurrently runs both commands in a single terminal, making development much more convenient.

If you’re starting a new TypeScript project, you can also consider tools like ts-node-dev, tsx, or nodemon with ts-node, which allow you to run TypeScript directly without a separate compilation step. These tools often provide a faster and simpler development experience while still supporting automatic reloads.

Be the first to post a comment.

Add a comment