Ask coding questions

← Back to all posts
Detect NodeJS Repl Exit
h
PowerCoder (690)

I want to do some cleanup actions before NodeJS exits. I've tried to do all of the exit event handlers but they don't seem to work.

Answered by Coder100 (10843) [earned 5 cycles]
View Answer
Commentshotnewtop
Coder100 (10843)

Why not configure you run button instead smh

# .replit
run="sh main.sh"
node .
# ok now the process has exited so you can do something like:
echo program stopped have a great day bye
PowerCoder (690)

@Coder100 I want something that automatically runs Node.js when the process exits.

Coder100 (10843)

yeah that automatically runs @PowerCoder

Coder100 (10843)

you can make it do like:

node .
node finished
// finished
console.log("Program stopped");

// add code

@PowerCoder

PowerCoder (690)

@Coder100 But I have to manually type that every time

Coder100 (10843)

@PowerCoder Do I need to give you an example?

SixBeeps (3218)

Do you mean when the server terminates, or a client disconnects?

PowerCoder (690)

@SixBeeps When the server terminates.