yuanchuan / node-watch

A wrapper and enhancements for fs.watch
https://npm.im/node-watch
MIT License
339 stars 44 forks source link

Warning when watching files #90

Closed ExperiBass closed 4 years ago

ExperiBass commented 4 years ago
(node:68807) DeprecationWarning: (node-watch) First param in callback function  is replaced with event name since 0.5.0, use  `(evt, filename) => {}` if you want to get the filename

Just a warning, no biggie

yuanchuan commented 4 years ago

Could you show me the code?

ExperiBass commented 4 years ago

https://github.com/yuanchuan/node-watch/blob/a0cc1646a18f8188b0b4d1c747fc7e98f67fc380/lib/watch.js#L176

looks like its triggered here: https://github.com/yuanchuan/node-watch/blob/a0cc1646a18f8188b0b4d1c747fc7e98f67fc380/lib/watch.js#L349

yuanchuan commented 4 years ago

Sorry I was trying to ask for the application code which produced the warning.

The warning is necessary as the given parameter to the callback function possibly be wrong.

ExperiBass commented 4 years ago

oh, here it is:

watcher.on('change', function() { // set up playing status watcher...
    game = JSON.parse(fs.readFileSync('./game.json', 'utf8'))
    bot.user.setActivity(game.activity) // ...and change the playing status as its updated
})
yuanchuan commented 4 years ago

The above code will not trigger the warning unless you give exactly one argument to the callback:

watcher.on('change', function(name) {

})

Which should be

watcher.on('change', function(event, name) {

})

[edit]

There are two ways to listen to change

callback style

const watch = require('node-watch');
watch(__dirname,  function(evt, name) {

});

event style

const watch = require('node-watch');
watch(__dirname).on('change', function(evt, name) {

}):

The warning will appear only using callback style once the given argument is exactly ONE. That is:

watch(__dirname, function(name) {

});

So the deprecation warning will never be seen in your code. The warning is necessary anyway and it's by design, at least in the current version.

ExperiBass commented 4 years ago

oh aight, ima close this then. thanks!