[英]Chodikar watch works only once
I'm rather new with Node.js. 我对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' 我尝试使用fs.watch,但返回的事件太多,所以我现在尝试使用“ 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 !
当我修改文件“ test.txt”时,我第一次在控制台上看到消息,但之后再也没有! As if the watcher was removed after the first event ...
好像观察者在第一个事件后被删除了...
The server run under Linux and I installed chokidar today 该服务器在Linux下运行,我今天安装了chokidar
What I'm doing wrong ? 我做错了什么?
I've tested the code youve submitted on linux and it work fine. 我已经测试了您在linux上提交的代码,并且可以正常工作。
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: 请注意,此模块的linux和mac os并非所有测试都通过了:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.