Jquery

Browse Files and Folders with Node.js

1 Mins read

Watching a file or directory for changes is an important part of automation.  We all enjoy using our favorite CSS preprocessor’s “watch” feature — we can still refresh the page and see our changes as though we were simply writing in pure CSS.  Node.js makes both file and directory watching easy — but it’s a bit more difficult than you may think.

Simply put:  Node.js’ watching features aren’t consistent or performant yet, which thedocumentation admits.  The good news:  a utility called chokidar stabilizes file watching and provides added insight into what has happened.  chokidar provides a wealth of listeners;  instead of providing boring reduced examples, here’s what chokidar provides you:

var chokidar = require('chokidar');

var watcher = chokidar.watch('file, dir, or glob', {
  ignored: /[\/\\]\./, persistent: true
});

var log = console.log.bind(console);

watcher
  .on('add', function(path) { log('File', path, 'has been added'); })
  .on('addDir', function(path) { log('Directory', path, 'has been added'); })
  .on('change', function(path) { log('File', path, 'has been changed'); })
  .on('unlink', function(path) { log('File', path, 'has been removed'); })
  .on('unlinkDir', function(path) { log('Directory', path, 'has been removed'); })
  .on('error', function(error) { log('Error happened', error); })
  .on('ready', function() { log('Initial scan complete. Ready for changes.'); })
  .on('raw', function(event, path, details) { log('Raw event info:', event, path, details); })

// 'add', 'addDir' and 'change' events also receive stat() results as second
// argument when available: http://nodejs.org/api/fs.html#fs_class_fs_stats
watcher.on('change', function(path, stats) {
  if (stats) console.log('File', path, 'changed size to', stats.size);
});

// Watch new files.
watcher.add('new-file');
watcher.add(['new-file-2', 'new-file-3', '**/other-file*']);

// Un-watch some files.
watcher.unwatch('new-file*');

// Only needed if watching is `persistent: true`.
watcher.close();

// One-liner
require('chokidar').watch('.', {ignored: /[\/\\]\./}).on('all', function(event, path) {
  console.log(event, path);
});

What a wealth of handles, especially when you’ve experienced the perils of `fs` watch functionality.  File watching is essential to seamless development and chokidar makes life easy!

About author
I’m a Web Interactive Developer, graduated from Kathir College, with a passion for front-end web development. I enjoy keeping up with new technologies and making the web awesome. I quickly found my passion was in creating a great user experience. Right now i am working at a company who is passionate about their work and to improve my skills by learning from them.
Articles
Related posts
Jquery

Building Your First Desktop App With HTML,Node-WebKit and JS

5 Mins read
These days you can do pretty much anything with JavaScript and HTML. Thanks to Node-WebKit, we can even create desktop applications that…
Jquery

Node.js: Five Things Every PHP Developer Should Know

3 Mins read
I recently started working on a few Node.js applications. Coming most recently from PHP (and Drupal in particular), I found the transition…

Leave a Reply

Your email address will not be published. Required fields are marked *