ts-node-dev not restarting when changes made
Asked Answered
W

5

12

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.

Warmedover answered 17/6, 2020 at 16:52 Comment(0)
W
39

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.

Warmedover answered 18/6, 2020 at 10:31 Comment(1)
Can confirm that this was the issue for me too, thank you!Ophidian
L
21

--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"
    },
Laurilaurianne answered 25/9, 2021 at 5:49 Comment(0)
F
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

Flavorful answered 8/1, 2021 at 3:47 Comment(0)
R
2

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"
  }
}

Retroflexion answered 3/1, 2022 at 0:5 Comment(0)
B
-1

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();
Biting answered 5/5, 2022 at 15:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.