Chokidar: Ability to ignore watching directories. Watch only files in directory

Created on 18 Sep 2018  路  1Comment  路  Source: paulmillr/chokidar

Hello!
Thanks for library!

How can I ignore all directories(directory1 and directory2) and watching only files(file1 and file2) in directory root?

  • root

    • directory1

    • directory2

    • file1

    • file2

e.g.

var chokidar = require('chokidar');
var fs = require('fs');

chokidar.watch('./root', {
  ignoreDirectories: true
}).on('all', (event, path) => {
  console.log(event, path);
});

Best regards!

Most helpful comment

Found answers by myself :)

Does the path is directory or file?

1.

var chokidar = require('chokidar');
var fs = require('fs');

chokidar.watch('./root', {
  alwaysStat: true
}).on('all', (event, path, stats) => {
  console.log(event, path, stats);
  console.log(`${path} is directory:`, stats.isDirectory());
});

Watch only files and ignore directories.

2.

var chokidar = require('chokidar');
var fs = require('fs');

chokidar.watch('./root', {
  ignored: (path, stats) => {
    return stats && stats.isDirectory();
  }
}).on('all', (event, path) => {
  console.log(event, path);
});

Please add more examples.
Thanks for library :)

>All comments

Found answers by myself :)

Does the path is directory or file?

1.

var chokidar = require('chokidar');
var fs = require('fs');

chokidar.watch('./root', {
  alwaysStat: true
}).on('all', (event, path, stats) => {
  console.log(event, path, stats);
  console.log(`${path} is directory:`, stats.isDirectory());
});

Watch only files and ignore directories.

2.

var chokidar = require('chokidar');
var fs = require('fs');

chokidar.watch('./root', {
  ignored: (path, stats) => {
    return stats && stats.isDirectory();
  }
}).on('all', (event, path) => {
  console.log(event, path);
});

Please add more examples.
Thanks for library :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Pomax picture Pomax  路  3Comments

j-fliegenschmidt picture j-fliegenschmidt  路  6Comments

jwahlin picture jwahlin  路  7Comments

ORESoftware picture ORESoftware  路  8Comments

bnainar picture bnainar  路  7Comments