Search code examples
node.jswindowschokidar

The same pattern works with chokidar-cli but does not work with chokidar on windows


Hello I'm trying to setup chokidar on Windows. I got to work it with chokidar-cli, this is the command line that reports the events correctly:

 chokidar "D:\PATH\**\WATCHED_FOLDER\*.TXT"

But when I try to do the same thing with node index.js, with the following code, the events don't get reported.

const chokidar = require('chokidar');
chokidar
    .watch('D:\\PATH\\**\\WATCHED_FOLDER\\*.TXT')
    .on('all', (event, path) => {
        console.log(event, path)
    })

I wonder why the same glob expression works on the command line and not in javascript. Also I did not find a reference on the syntax accepted by chokidar on Windows.

Can someone point out the problem here ? Thanks.


Solution

  • While chokidar-cli works with backslashes, chokidar requires forward slashes in the glob path. This works:

    const chokidar = require('chokidar');
    chokidar
        .watch('D:/PATH/**/WATCHED_FOLDER/*.TXT')
        .on('all', (event, path) => {
            console.log(event, path)
        })
    

    But beware, the string in the variable path is in still with backslashes: e.g. 'D:\\PATH\\SUBFOLDER\\WATCHED_FOLDER\\TEST.TXT'. This is somehow inconsistent, but it works.