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 🙏

© 2024 – Pkg Stats / Ryan Hefner

module-monitor

v1.0.1

Published

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

Downloads

10

Readme

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.