Home  Typescript   Difference ...

difference between ts-node-dev and nodemon

To add a watch mode to TypeScript project using Node.js, you can use a tool like ts-node-dev or nodemon. These tools automatically restart your application whenever you make changes to your TypeScript files. Here's how to set it up:

Using ts-node-dev

  1. Install ts-node-dev:

    npm install ts-node-dev --save-dev
    
  2. Update package.json: Add a watch script to your package.json file.

    {
      "scripts": {
        "build": "tsc",
        "start": "node dist/index.js",
        "dev": "ts-node-dev --respawn --transpile-only src/index.ts"
      }
    }
    
  3. Run the watch script:

    npm run dev
    

Using nodemon

  1. Install nodemon and ts-node:

    npm install nodemon ts-node --save-dev
    
  2. Create a nodemon.json file: Create a nodemon.json configuration file in the root of your project to specify how nodemon should handle TypeScript files.

    {
      "watch": ["src"],
      "ext": "ts",
      "exec": "ts-node ./src/index.ts"
    }
    
  3. Update package.json: Add a watch script to your package.json file.

    {
      "scripts": {
        "build": "tsc",
        "start": "node dist/index.js",
        "dev": "nodemon"
      }
    }
    
  4. Run the watch script:

    npm run dev
    
Published on: Jun 26, 2024, 05:53 AM  
 

Comments

Add your comment