Nodemon runs multiple times on save when using babel

↘锁芯ラ 提交于 2019-12-11 06:26:35

问题


I am using Nodemon and Babel 6. I have uninstalled Nodemon globally and have installed the latest version.

I have tried both of the following lines in my npm scripts:

"watch-new": "nodemon src/index.js --exec babel-node"

And

"watch-new": "nodemon src/index.js --exec babel -w src/ -d build/ --source-maps"

Changes in running babel seem to result in similar/identical nodemon-related mess-ups.

I started with a file that simply contained console.log("Hello, World!"); then added a line after that statement that contained console.log("Test");. Saving (only once) the now two line file resulted in the following console output.

Adding either of the following to nodemon makes my program restart only once "--delay 2.5" or "-L" / "--legacyWatch"

Why is it running and reloading so many times?


回答1:


I've found that combinations of nodemon, babel, and other watcher type programs can often oddly interact. This type of "double run" outcome is pretty common. They work great when set up just so, but can be fragile.

That said, I do not understand your choice of command line arguments. I won't say they're wrong, but they don't correspond to the versions of babel that I have installed, or the documentation I'm using. There have been notable changes in the recommended ways to run Babel; not all the documentation on the internet is current and up-to-date with the latest approaches.

So rather than trying to debug what isn't working in your configuration, let me give you a configuration that is working for me.

First I installed Babel and its presets:

npm install --save-dev babel-cli babel-preset-es2015

Then the npm script in package.json:

"watch-new": "nodemon src --exec babel --presets es2015 -w src/ --out-dir build/ --source-maps"

(You can use other presets. I use just the big one, ES2015.)

I invoke nodemon against a source directory, not a single source file, in other to keep babel from nesting src under build; I assume you want individual .js files in build, not a src subdirectory. I also use the --out-dir option, which I understand to be correct (not -d).

Finally, run it:

npm run watch-new

These packages and that script invocation give correct, rerun-once-per-change behavior for me. If you want node to run the code immediately after transformation instead of saving it under build/, you can change --exec babel to --exec babel-node and remove the --out-dir specification.

As a final note, your question is tagged for ECMAscript-6. What was once called es6 is now more officially called es2015. (See e.g. this primer on the new version names.) You're more likely to get the correct, up-to-date documentation if you search under the new es2015 term.



来源:https://stackoverflow.com/questions/39340268/nodemon-runs-multiple-times-on-save-when-using-babel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!