fs.watch fired twice when I change the watched file

后端 未结 12 1459
星月不相逢
星月不相逢 2020-12-09 01:30
 fs.watch( \'example.xml\', function ( curr, prev ) {
   // on file change we can read the new xml
   fs.readFile( \'example.xml\',\'utf8\', function ( err, data ) {         


        
12条回答
  •  执笔经年
    2020-12-09 02:11

    I suggest to work with chokidar (https://github.com/paulmillr/chokidar) which is much better than fs.watch:

    Commenting its README.md:

    Node.js fs.watch:

    • Doesn't report filenames on OS X.
    • Doesn't report events at all when using editors like Sublime on OS X.
    • Often reports events twice.
    • Emits most changes as rename.
    • Has a lot of other issues
    • Does not provide an easy way to recursively watch file trees.

    Node.js fs.watchFile:

    • Almost as bad at event handling.
    • Also does not provide any recursive watching.
    • Results in high CPU utilization.

提交回复
热议问题