使用babel时,Nodemon会多次运行 [英] Nodemon runs multiple times on save when using babel
问题描述
我已经在我的npm脚本中尝试过以下两行:
watch-new:nodemon src / index.js --exec babel-node
和
watch-new:nodemon src / index.js --exec babel -w src / -d build / --source-maps
运行babel的更改似乎会导致类似/相同的nodemon相关的混乱。
我从一个文件开始, console.log(Hello,World!);
然后在包含 console.log(Test);
。保存(只有一次)现在的两行文件导致以下控制台输出。
将以下任一项添加到nodemon中,使我的程序仅重新启动一次--delay 2.5 或-L/--legacyWatch
为什么运行和重新加载这么多次?
我发现 nodemon
, babel
,其他观察者类型的程序可能经常是奇怪的交互。这种双运行的结果是很常见的。他们在设置 时工作得很好,但可能很脆弱。
说,我不明白你选择的命令行参数。我不会说他们错了,但是它们与我已经安装的 babel
的版本不符,或者是我使用的文档。推荐运行巴别塔的方法有显着的变化;不是所有的互联网上的文档都是最新的,最新的方法。
所以而不是试图调试你的配置不起作用,让我给你一个 为我配置的配置。
首先我安装了Babel及其预设:
npm install --save-dev babel-cli babel-preset-es2015
/ pre>
然后在
package.json
中的npm脚本:watch-new:nodemon src --exec babel --presets es2015 -w src / --out-dir build / --source-maps
(您可以使用其他预设,我只使用较大的一个, ES2015 。)
我调用
nodemon
针对源目录,而不是单个源文件,另外要保留babel
从嵌套src
在build $下C $ C>;我假设你想在
build
中单独.js
文件,而不是src
子目录。我也使用我理解为正确的- out-dir
选项(不是-d
)。 / p>
最后运行它:
npm运行watch-new
这些包和脚本调用给出正确的,每次重新运行一次我的行为如果您希望
节点
在转换后立即运行代码,而不是将其保存在build /
下,您可以更改- exec babel
to- exec babel-node
并删除- out-dir
规格。
最后一点,您的问题被标记为ECMAscript-6。曾经称为
es6
现在更正式地称为es2015
。 (参见例如新版本名称上的这个引文)。如果您在新的es2015
下搜索,则更有可能获取正确的最新文档。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 containedconsole.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?
解决方案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 keepbabel
from nestingsrc
underbuild
; I assume you want individual.js
files inbuild
, not asrc
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 underbuild/
, 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 calledes2015
. (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 newes2015
term.这篇关于使用babel时,Nodemon会多次运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!