Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including node-watch with all npm packages installed. Try it out:

var nodeWatch = require("node-watch")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

node-watch v0.5.5

A wrapper and enhancements for fs.watch

node-watch Status

A wrapper and enhancements for fs.watch (with 0 dependencies).

NPM

Installation

npm install node-watch

Example

var watch = require('node-watch');

watch('file_or_dir', { recursive: true }, function(evt, name) {
  console.log('%s changed.', name);
});

This is a completely rewritten version, much faster and in a more memory-efficient way. So with recent nodejs under OS X or Windows you can do something like this:

// watch the whole disk
watch('/', { recursive: true }, console.log);

Why?

  • Some editors will generate temporary files which will cause the callback function to be triggered multiple times.
  • When watching a single file the callback function will only be triggered once.
  • Missing an option to watch a directory recursively.
  • Recursive watch is not supported on Linux or in older versions of nodejs.

Events

The events provided by the callback function is either update or remove, which is less confusing to fs.watch's rename and change.

watch('./', function(evt, name) {

  if (evt == 'update') {
    // on create or modify
  }

  if (evt == 'remove') {
    // on delete
  }

});

Options

The usage and options of node-watch is fully compatible with fs.watch.

  • persistent: <Boolean> default = true
  • recursive: <Boolean> default = false
  • encoding: <String> default = 'utf8'
Extra options
  • filter: <RegExp | Function> filter using regular expression or custom function.
// watch only for json files
watch('./', { filter: /\.json$/ }, console.log);

// ignore node_modules
watch('./', {
  recursive: true,
  filter: function(name) {
    return !/node_modules/.test(name);
  }
}, console.log);

Watcher object

The watch function returns a fs.FSWatcher like object as the same as fs.watch (>= v0.4.0).

var watcher = watch('./', { recursive: true });

watcher.on('change', function(evt, name) {
  // callback
});

watcher.on('error', function(err) {
  // handle error
});

// close
watcher.close();

// is closed?
watcher.isClosed()

The watcher object is also an instance of EventEmitter. This's a list of methods for watcher specifically:

  • .on
  • .once
  • .emit
  • .close
  • .listeners
  • .setMaxListeners
  • .getMaxListeners
Extra methods
  • .isClosed detect if the watcher is closed

Known issues

Windows, node < v4.2.5

  • Failed to detect remove event
  • Failed to get deleted filename or directory name

Misc

1. Watch multiple files or directories in one place

watch(['file1', 'file2'], console.log);

2. Customize watch command line tool

#!/usr/bin/env node

// https://github.com/nodejs/node-v0.x-archive/issues/3211
require('epipebomb')();

var watcher = require('node-watch')(
  process.argv[2] || './', { recursive: true }, console.log
);

process.on('SIGINT', watcher.close);

Monitoring chrome from disk:

$ watch / | grep -i chrome

License

MIT

Copyright (c) 2012-2017 yuanchuan

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free