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


Version published
Maintainers
1
Created

Package description

What is node-watch?

The node-watch package is a simple and efficient file system watcher for Node.js. It allows you to monitor changes in files and directories, making it useful for tasks such as live-reloading, automated testing, and more.

What are node-watch's main functionalities?

Watch a single file

This feature allows you to watch a single file for changes. The callback function is triggered whenever the file is modified.

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

watch('file.txt', { recursive: false }, function(evt, name) {
  console.log('%s changed.', name);
});

Watch a directory recursively

This feature allows you to watch a directory and all its subdirectories for changes. The callback function is triggered whenever any file within the directory or its subdirectories is modified.

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

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

Filter files to watch

This feature allows you to specify a filter to watch only certain types of files. In this example, only JavaScript files within the directory are monitored for changes.

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

watch('directory', { filter: /\.js$/ }, function(evt, name) {
  console.log('%s changed.', name);
});

Other packages similar to node-watch

Readme

Source

node-watch

A neat fs.watch wrapper.

NPM

Installation

npm install node-watch

Example

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

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

This is a completely rewritten version, much faster and in a more memory-efficient way. So with recent nodejs versions 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.

Notice

  • The recursive option is defaults to be false since v0.5.0.
  • Parameters in the callback function always provide event name since v0.5.0.

Events

The events provided by the callback function would be either update or remove.

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

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

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

});

Watcher object

watch function returns a fs.FSWatcher like object as the same as fs.watch.

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

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

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

// close
watcher.close();

Extra options

  • filter: Filter files or directories or skip to watch them.
var options = {
  recursive: true,
  filter : function(name) {
    return !/node_modules/.test(name);
  }
};

// ignore node_modules
watch('mydir', options, console.log);

Other ways to filter

a) filtering directly inside the callback function:

watch('./', { recursive: true }, function(evt, name) {
  // ignore node_modules
  if (!/node_modules/.test(name)) {
    // do something
  }
});

b) filtering with higher order function:

function filter(pattern, fn) {
  return function(evt, name) {
    if (pattern.test(name)) {
      fn(evt, name);
    }
  }
}

// watch only for js files
watch('.', filter(/\.js$/, console.log));

Misc

1. Watch mutiple files or directories in one place
watch(['file1', 'file2'], console.log);
2. Catch errors after deleting a watched directory on Windows
watch('somedir', console.log)
  .on('error', function() {
    // ignore it if you wish.
  });

Keywords

FAQs

Last updated on 12 Mar 2017

Did you know?

Socket

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc