简体   繁体   中英

Chodikar watch works only once

I'm rather new with Node.js. I would like to "watch" a file containing some parameters to be able to reload it automatically when change are made.

I tried with fs.watch, but it was returning too many event, so I try now with 'chokidar'

console.log('Server running');
var chokidar = require('chokidar');
var watcher = chokidar.watch('test.txt',{ persistent: true });
watcher.on("change", function(path) {
    var d = new Date();
    var n = d.toUTCString();
    console.log(n + " : start update");
    console.log(path + " was changed");
});

The issue is that it works only once !! When I modify the file 'test.txt', I have the message on the console the first time, but never again after ! As if the watcher was removed after the first event ...

The server run under Linux and I installed chokidar today

What I'm doing wrong ?

I've tested the code youve submitted on linux and it work fine.

I advise you to add the on error event listener as follows:

console.log('Server running'); 
var chokidar = require('chokidar'); 
var watcher = chokidar.watch('test.txt',{ persistent: true });     
watcher.on("change", function(path) {
var d = new Date();
    var n = d.toUTCString();
    console.log(n + " : start update");
    console.log(path + " was changed"); }); 
watcher.on('error', error => log(`Watcher error: ${error}`))

Pay notice that the not all of the tests are passing for linux and mac os for this module:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM