Socket
Socket
Sign inDemoInstall

node-watch

Package Overview
Dependencies
0
Maintainers
1
Versions
40
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    node-watch

fs.watch() wrapper of Nodejs


Version published
Weekly downloads
458K
increased by2.1%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

#Node-watchj A fs.watch wrapper to watch files or directories(recursively by default).

NPM

Installation

npm install node-watch

Example

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

watch('somedir_or_somefile', function(filename) {
  console.log(filename, ' changed.');
});

Why fs.watch wrapper

  • 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 one time and then is seem to be unwatched.
  • Missing an option to watch a directory recursively.

The difference

This module currently does not differentiate event like rename or delete. Once there is a change, the callback function will be triggered.

Options

recursive:Watch it recursively or not (defaults to true).

followSymLinks: Follow symbolic links or not (defaults to false).

maxSymLevel: The max number of following symbolic links, in order to prevent circular links (defaults to 1).

filter: node-watch will only watch elements that pass the test implemented by the provided function. The filter function is provided with a full path string argument(defaults to (fullPath)=>true ).

watch('somedir', { recursive: false, followSymLinks: true }, function(filename) {
  console.log(filename, ' changed.');
});

Watcher object

Since v0.4.0 watch() will return a [fs.FSWatcher](fs.FSWatcher like object, so you can close the watcher or detect change by change event instead of the old callback function.

var watcher = watch('./');

watcher.on('change', function(file) {
  //
});

watcher.on('error', function(err) {

});

// close
watcher.close();

###FAQ

1. How to watch mutiple files or directories
watch(['file1', 'file2'], function(file) {
  //
});
2. How to filter files

You can write your own filter function as a higher-order function. For example:

var filter = function(pattern, fn) {
  return function(filename) {
    if (pattern.test(filename)) {
      fn(filename);
    }
  }
}
// only watch for js files
watch('mydir', filter(/\.js$/, function(filename) {
  //
}));

Alternatively, supply a filter function in the options object. For example:

// don't watch node_modules folder
var options = {
  filter : function(filename) {
    return !/node_modules/.test(filename);
  }
};
watch('mydir', options, function(filename) {
  //
}));

The second approach helps avoiding the max open files limit

Keywords

FAQs

Last updated on 25 Jul 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc