npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

filemonitor

v0.1.3

Published

Monitor filesystem events in node.js. Watch for file changes and run callbacks.

Downloads

28

Readme

#filemonitor-node

This node module is a thin wrapper around the inotifywait command. It can be used to monitor and act upon file system events. Filemonitor-node can be used to monitor events on files or directories. When a directory is monitored, events are returned for the directory itself and its files.

##Requirements

Filemonitor-node depends on inotify-tools. To install inotify-tools on a Linux (Debian/Ubuntu) system:

sudo apt-get install inotify-tools

For installing on other linux flavors, go here.

##Installation

On your project directory:

npm install filemonitor

##Events

The following events are captured by the module:

  • access: A watched file or a file within a watched directory was read from.
  • modify: A watched file or a file within a watched directory was written to.
  • attrib: The metadata of a watched file or a file within a watched directory was modified. This includes timestamps, file permissions, extended attributes etc.
  • close_write: A watched file or a file within a watched directory was closed, after being opened in writeable mode. This does not necessarily imply the file was written to.
  • close_nowrite: A watched file or a file within a watched directory was closed, after being opened in read-only mode.
  • close: A watched file or a file within a watched directory was closed, regardless of how it was opened. Note that this is actually implemented simply by listening for both close_write and close_nowrite, hence all close events received will be output as one of these, not CLOSE.
  • open: A watched file or a file within a watched directory was opened.
  • moved_to: A file or directory was moved into a watched directory. This event occurs even if the file is simply moved from and to the same directory.
  • moved_from: A file or directory was moved from a watched directory. This event occurs even if the file is simply moved from and to the same directory.
  • move: A file or directory was moved from or to a watched directory. Note that this is actually implemented simply by listening for both moved_to and moved_from, hence all close events received will be output as one or both of these, not MOVE.
  • move_self: A watched file or directory was moved. After this event, the file or directory is no longer being watched.
  • create: A file or directory was created within a watched directory.
  • delete: A file or directory within a watched directory was deleted.
  • delete_self: A watched file or directory was deleted. After this event the file or directory is no longer being watched. Note that this event can occur even if it is not explicitly being listened for.
  • unmount: The filesystem on which a watched file or directory resides was unmounted. After this event the file or directory is no longer being watched. Note that this event can occur even if it is not explicitly being listened to.

##Usage

To monitor a file is as easy as:

    var filemon = require('filemonitor');
    
    var onFileEvent = function (ev) {
      var filetype = ev.isDir ? "directory" : "file";
      console.log("Event " + ev.eventId + " was captured for " + filetype + " " + ev.filename + " on time: " + ev.timestamp.toString());
    }

    var options = {
      target: "./docs/myfile.txt",
      listeners: {
        all_events: onFileEvent
      }
    }
    
    filemon.watch(options);

In the previous example, the module was instantiated. Then, an options object is set including a target file and a callback function in case an event is captured. Finally, filemon.watch(options) is called to start monitoring.

###Event Object Each time an event is captured, its callback is called passing an event object as argument. This object has the following properties:

  • filename is a string with the relative path to the watched file/directory.
  • eventId is a string whose value is one of the events names described in Events section.
  • isDir is true if what is being watched is a directory. Otherwise, false.
  • timestamp is a Date object. It has the datetime at which the event was captured.

###Options The options to configure filemonitor-node include:

  • target is an array of strings containing files/directories to be watched. (default: current directory ["."]).
  • recursive is a boolean. When true, nested files in the subdirectories of the target directories will be also watched (recursively). Normally, these nested files are excluded from any monitoring. (default: false).
  • exclude is an array of string containg files/directories to be excluded from watching. This is very useful when watching directories. (default: empty array []).
  • dismissQueuedEvents is true for dismissing events that are already pending. If a previously captured event has not been. (default: true).
  • listeners is an object for mapping events to callbacks. Listener's keys can be any of the event names described in section Events. Each of these keys must have assigned a callback as a value. The special key all_events matches a callback to all event. (default: empty object {}).

###More examples

For monitoring a complete directory waiting for newly created files:

    var filemon = require('filemonitor');
    
    var onFileEvent = function (ev) {
      console.log("File " + ev.filename + " triggered event " + ev.eventId + " on " + ev.timestamp.toString());
    }

    var onFileCreation = function (ev) {
      console.log("File " + ev.filename + " was created on " + ev.timestamp.toString());
    }

    var options = {
      recursive: false,
      target: "./docs/",
      listeners: {
        all_events: onFileEvent,
        create: onFileCreation
      }
    }
    
    filemon.watch(options);

Another example for watching multiple files and excluding some others:

    var filemon = require('filemonitor');
    
    var onFileChange = function (ev) {
      //... Do Something
    }

    var onFileCreation = function (ev) {
      //... Do Something
    }

    var options = {
      recursive: false,
      target: ["./docs/", "./config/app.config"],
      exclude: ["./docs/myfile1.txt", "./docs/myfile10.txt"],
      listeners: {
        modify: onFileChange,
        create: onFileCreation
      }
    }
    
    filemon.watch(options);

###Other comments...

  • Once the watch function has been called, options cannot be changed.
  • Monitoring can be stopped calling function filemon.stop().