Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodemon + babel restart the server multiple times

In my package.json I have a start-script which I'm using for my dev enviroment. It looks like this:

"scripts": {
    "dev": "NODE_PATH=src nodemon --watch src/ --exec babel-node src/app.js"
}

When I'm hitting npm run dev everything works fine and babel is transpiling everything as it should and nodemon starts watching. I see this:

[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: /Users/Jonathan/Documents/swissnet/src/**/*
[nodemon] starting `babel-node src/app.js`

When I'm saving files within the src/-folder nodemon will restart the server. But here's my issue, It restarts 2-3 times... Everytime I save a file it looks like this:

[nodemon] restarting due to changes...
[nodemon] starting `babel-node src/app.js`
[nodemon] restarting due to changes...
[nodemon] starting `babel-node src/app.js`
[nodemon] restarting due to changes...
[nodemon] starting `babel-node src/app.js`

If I enter "rs" then nodemon restarts, once, as expected.

I have no idea how to solve this, or even where to search for answers. I've tried to google it. I've been visiting the bug section of the package on github... (Maybe I just suck at googling).
Here's the only link I've found for the same issue, but it doesn't seem to have an answer: Nodemon runs multiple times on save when using babel.
I tried his script anyway NODE_PATH=src nodemon src --exec babel -w src/ --out-dir build/ --source-maps but the same thing happened, restarting twice or thrice.

Like @Connorelsea said in the comment section of the answer provided in the link above, if I add --delay 2.5 it restart only once.

I'm thinking maybe when I hit save in a watched file, nodemon restarts instantly and babel start transpiling. When babel is done it saves a bunch om transpiled files and nodemon restarts once again since changes to the src/-folder was made. But I have no idea how to debug this.

Hope you guys can help me!

**** EDIT ****

Just found this https://github.com/remy/nodemon/issues/508 but the only solutions they have is to "upgrade nodemon". I do have the latest which is 1.11.0 at this time.

like image 337
Jonathan Nielsen Avatar asked Oct 07 '16 12:10

Jonathan Nielsen


Video Answer


3 Answers

So, now a couple of months later i figured out what's wrong. Seems like the server simply restarts once when I save and the once again when babel transformed the code a couple of seconds later since the files get updated. So it was the package babel-node that was giving me this unwanted behaviour. It works with a nodemon delay of 2 seconds --delay 2 or more.

like image 85
Jonathan Nielsen Avatar answered Oct 12 '22 23:10

Jonathan Nielsen


You should use babel-node as an executor like this:

nodemon ./index.js --exec babel-node
like image 40
chuyik Avatar answered Oct 13 '22 00:10

chuyik


So in case someone stumbles on this like I did.

While the delay works, it works because your build takes less than 2s usually.

This can lead to it being flakey, or just take longer than it needs.

The correct solution is to actually ignore the output directory, or file within nodemon.

NODE_PATH=src nodemon --watch src/ --exec babel-node src/app.js --out-file dist/app.js -- dist/app.js

like image 5
Amos47 Avatar answered Oct 12 '22 22:10

Amos47