Nodemon + Babel多次重新启动服务器 [英] Nodemon + babel restart the server multiple times

查看:310
本文介绍了Nodemon + Babel多次重新启动服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 package.json 中,有一个用于我的开发环境的开始脚本.看起来像这样:

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

当我按下npm run dev时,一切正常,而babel则按需转译了所有内容,nodemon开始观看.我看到了:

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`

当我在src/-folder节点中保存文件时,monmon将重新启动服务器.但是,这是我的问题,它会重新启动2-3次...每次保存文件时,它看起来都像这样:

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`

如果我输入"rs",那么nodemon将按预期方式重新启动一次.

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

我不知道如何解决这个问题,甚至不知道在哪里寻找答案.我试图用谷歌搜索它.我一直在github上访问软件包的bug部分...(也许我只是在谷歌搜索上很烂).
这是我针对同一问题找到的唯一链接,但似乎没有答案:

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.

就像@Connorelsea在上面的链接中提供的答案的评论部分所述,如果我添加--delay 2.5,它将仅重新启动一次.

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.

我在想,也许当我在观看文件中单击保存"时,nodemon立即重新启动,而babel开始转码. babel完成后,它将保存一堆om转译的文件,由于对src/-folder进行了更改,nodemon再次重新启动.但是我不知道如何调试它.

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.

希望你们能帮助我!

****编辑****

**** EDIT ****

刚刚找到了这个 https://github.com/remy/nodemon/issues/508,但是他们唯一的解决方案是升级nodemon".我目前的最新版本是1.11.0.

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.

推荐答案

所以,几个月后,我发现了问题所在.好像服务器在我保存时仅重新启动一次,而babel在文件更新后几秒钟后转换代码又重新启动了一次. 因此,正是软件包babel-node给了我这种不必要的行为. nodemon的延迟时间为2秒--delay 2或更长.

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.

这篇关于Nodemon + Babel多次重新启动服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆