module-monitor

1.0.1 • Public • Published

Modul-Monitor facilitates automatic reloading of modules in Node.js applications, ensuring real-time updates during development.

Report Bug · Request Feature

Table of Contents
  1. About The Project
  2. Getting Started
  3. Warning
  4. Contributing
  5. License

About The Project

Modul-Monitor is a lightweight utility for Node.js applications that enables automatic reloading of modules when they are modified. It provides a seamless development experience by monitoring specified directories for file changes and refreshing the modules accordingly, ensuring that the latest code changes are always reflected during development.

Getting Started

First of all you have to Include this Module into your Project, here's how to Include Module-Monitor.

With CommonJS:

const { WatchFile, WatchFolder } = require("module-monitor");

With ES6:

import { WatchFile, WatchFolder } from "module-monitor";

Then you can use it as an example below:

// Check that config.js has been changed or not using WatchFile
let config = new WatchFile("./config.js");

// Then you can also use the Callback Event as an example:
config.setOnReload((module, err) => {
  if (err) {
    console.error("Failed to reload:", err);
  } else {
    let getConfig = module.getConfig();
    console.log(`Module Reloaded: ${getConfig}`);
  }
});

// Monitor Folder using WatchFolder if there is Module Addition or Module Deletion
let commands = {}; // A new module will be in this collection
let commandsWatch = new WatchFolder("./commands", commands);

// then you can use the Callback Event
commandsWatch.setOnAdd((filePath) => {
  console.log(`New Module in: ${filePath}`);
});

Callback Events

In Module-Monitor there are a total of 4 Callback Events but 1 specifically for WatchFolder only.

and these are the Callback Events available on the Module-Monitor:

Reload Module Event

This event works if you want to check if the Module that was changed or added was successfully loaded or not.

watcher.setOnReload((module, err) => {
  // module: is the Module that was just loaded and Imported
  // err: Err only exists when the Module cannot be loaded
});

Module Change Event

This event works when there is a change in the Module being monitored.

watcher.setOnChange((filePath) => {
  // filePath: the location of the changed file in the Module
});

Deleted Module Event

this event works only when the Module is deleted

watcher.setOnDelete((filePath) => {
  // filePath: the location of the deleted file in the Module
});

Add Module Event

This event only works when a new Module is added and only works on WatchFolder.

watcher.setOnAdd((filePath) => {
  // filePath: the location of the new file in the Module
});

Warning

if you use setInterval or whatever on the Module you want to monitor, please use cleanupModule.

Usage Example:

// This is on the module that you want to Handle if there is a task or whatever it is, for example in message.js

let messages = [];
let counter = 1;
let interval = setInterval(function () {
  messages.push(`This is the message of ${counter}`);
  counter++;
}, 1000);

export async function cleanupModule() {
  clearInterval(interval);
}

So there will be no double intervals or spam.

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag enhancement. Don't forget to give the project a star! Thanks again!

License

Distributed under the MIT. See LICENSE for more information.

Package Sidebar

Install

npm i module-monitor

Weekly Downloads

1

Version

1.0.1

License

MIT

Unpacked Size

11.9 kB

Total Files

7

Last publish

Collaborators

  • xanderid