ts-node-dev not restarting when changes made

Here are my files :

package.json :

"scripts": {
  "generate-interfaces": "ts-node src/cli/generate-interfaces.ts",
  "dist": "npm run generate-interfaces && rm -rf dist && tsc && cp -r static/ dist/ && cp -r resource/ dist/",
  "prestart": "npm run generate-interfaces",
  "start": "ts-node-dev --respawn --transpileOnly --no-notify ./src/index.ts",
  "start:inspect": "ts-node-dev --no-deps --inspect -- ./src/index.ts",
  "pretest": "npm run generate-interfaces",
  "test": "jest"
 }

tsconfig.json

{
"compilerOptions": {
"declaration": true,
"target": "es2017",
"module": "commonjs",
"esModuleInterop": true,
"outDir": "dist",
"sourceMap": true,
"skipLibCheck": true,
"typeRoots": ["node_modules/@types", "./typings", "node_modules/**/*.d.ts"],
"lib": ["esnext"]
},
"include": ["src/**/*.ts", "./typings/**/*.d.ts"],
"exclude": ["node_modules/**", "dist"]
}

When I do any changes, I get the little popup, but it doesn’t actually restart the server not sure what I am doing wrong here.

Note: The first time I do changes after server restart manually it shows me a popup and something like this in the terminal [INFO] 22:07:09 Restarting: src/analytics-engine.ts has been modified after that no changes detection.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

The issue is resolved now. I found out the problem using --debug that it was an error related to ‘SIGTERM’. So I added a flag --exit-child in my npm start script.

Solution 2

--exit-child worked for me too.

Example in package.json

"scripts": {
        "start": "ts-node-dev --respawn --transpile-only --exit-child --watch src src/index.ts"
    },

Solution 3

I have something similar, using ts-node-dev and pino.

When the server starts, there are some logging to output server init process. When the dev server restarts by ts-node-dev, sometimes it spits out write: EPIPE error, from what I’m assuming is the restarted server trying to write to the old stream.

the --exit-child flag seems to have fixed the issue

Solution 4

you can try to put –respawn and –inspect flags in your package.json

Example:

{
  "scripts": {
    "start:dev": "tsnd --respawn --inspect --transpile-only --ignore-watch node_modules src/server"
  }
}

Solution 5

you can treat it in your index file:

this line:
process.on("SIGTERM", tearDown);

import {$log} from "@tsed/common";
import { PlatformExpress } from "@tsed/platform-express";
import {Server} from "./Server";

async function bootstrap() {
  try {
    const platform = await PlatformExpress.bootstrap(Server);
    await platform.listen();
  } catch (error) {
    $log.error({event: "SERVER_BOOTSTRAP_ERROR", error});
  }
}

const chalk = require("chalk");

process.stdin.resume();  

const tearDown = async () => {
  process.exit();
};

// // catch ctrl+c event and exit normally
process.on("SIGINT", tearDown);     

//catch uncaught exceptions, trace, then exit normally
process.on("uncaughtException", function (e) {
  console.log("Uncaught Exception...");
  console.log(e.stack);
  process.exit(99);
});

// catches "kill pid" (for example: nodemon restart)
process.on("SIGUSR1", tearDown);
process.on("SIGUSR2", tearDown);
process.on("SIGTERM", tearDown);

bootstrap();

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply