Nodemon + babel restart the server multiple times

Jonathan Nielsen picture Jonathan Nielsen · Oct 7, 2016 · Viewed 9.3k times · Source

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.

Answer

Jonathan Nielsen picture Jonathan Nielsen · Mar 6, 2017

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.