27 July 2026
StäfnNobody really does anymore
Even deno.ns only really made sense when Deno had its own ideas about a stdlib
fs.watch("./", { recursive: true, encoding: "utf-8" }, (eventType, fileName)=>{
if(eventType === "change"){
console.log("[RESTART] on file changes -", fileName);
exec("node ./src/index.ts", (error, stdout, stderr)=>{
console.log(error);
});
}
});
I was trying to build a Nodemon clone, and I have written the logic above to try restart the program when there is some change in the file contents. But the exec() part does not work. What am I doing wrong here?
dotnetfs.watch("./", { recursive: true, encoding: "utf-8" }, (eventType, fileName)=>{
if(eventType === "change"){
console.log("[RESTART] on file changes -", fileName);
exec("node ./src/index.ts", (error, stdout, stderr)=>{
console.log(error);
});
Where's the restart? All you're doing here is launching a new process.
WatzonWhere's the restart? All you're doing here is launching a new process.
I wanted to restart. I should have read the documentation properly. It says it only spawns or launches a new process
Super basic example:
import fs from "node:fs";
import { spawn, type ChildProcess } from "node:child_process";
let child: ChildProcess | undefined;
let timer: NodeJS.Timeout;
function restart() {
child?.kill();
child = spawn("npx", ["tsx", "./src/index.ts"], {
stdio: "inherit",
shell: true,
});
}
fs.watch("./src", { recursive: true }, (_eventType, filename) => {
if (!filename) return;
clearTimeout(timer);
timer = setTimeout(restart, 100);
});
restart();