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

mm_hot_reload

v1.3.4

Published

代码热重载管理工具

Readme

mm_hot_reload

中文 | English

一个用于Node.js的热重载模块,支持实时监听并自动重新加载修改过的JavaScript和JSON文件。

安装

npm install mm_hot_reload

特性

  • ✅ 支持JavaScript和JSON文件的热重载
  • ✅ 文件变化时自动重新加载
  • ✅ 支持文件添加、修改、删除事件
  • ✅ 可配置是否自动取消监听已删除的文件
  • ✅ 支持回调函数处理重载事件
  • ✅ 完善的错误处理和日志记录
  • ✅ 符合ES6+语法规范

基本用法

创建实例

const { HotReload } = require("mm_hot_reload");

// 创建实例,可传入配置参数
const hr = new HotReload({
  path: __dirname, // 监听的根路径
  watch: true, // 是否开启监听,默认true
  unwatch: false, // 文件删除时是否自动取消监听,默认false
  ext: [".js", ".json"], // 监听的文件扩展名
  dir: ["/app/*"], // 监听的目录模式
  log_level: "error", // 日志级别:error, warn, info, debug
  debounce_time: 0, // 防抖时间(毫秒)
  watch_mode: "non-polling", // 监听模式
});

加载模块

// 加载JavaScript模块
const myModule = hr.load("./my-module.js", (newModule, way, path) => {
  // 文件变化时的回调函数
  $.log.debug("模块更新方式:", way); // 'add', 'change', 或 'delete'
  $.log.debug("模块路径:", path);
  $.log.debug("新的模块内容:", newModule);
});

// 加载JSON配置文件
const config = hr.load("./config.json", (newConfig, way, path) => {
  $.log.debug("配置文件已更新:", newConfig);
});

卸载模块

hr.unload("./my-module.js", (path) => {
  $.log.debug("模块已卸载:", path);
});

重新加载模块

const reloadedModule = hr.reload("./my-module.js", (newModule, way, path) => {
  $.log.debug("模块已重新加载");
});

手动监听文件

hr.watch("./another-module.js", (newModule, way, path) => {
  $.log.debug("文件发生变化");
});

完整示例

const HotReload = require("mm_hot_reload");

// 创建实例
const hr = new HotReload({
  path: __dirname,
  watch: true,
  unwatch: true,
});

// 加载并监听配置文件
const config = hr.load("./config.json", (newConfig, way, path) => {
  if (way === "change") {
    $.log.debug("配置文件已更新:", newConfig);
    // 在这里处理配置更新逻辑
  } else if (way === "delete") {
    $.log.debug("配置文件已删除");
    // 处理配置文件删除的情况
  }
});

// 加载并监听业务模块
const businessModule = hr.load("./business.js", (newModule, way, path) => {
  if (way === "change") {
    $.log.debug("业务模块已更新");
    // 重新初始化业务逻辑
  }
});

// 手动监听其他文件
hr.watch("./utils.js", (newModule, way, path) => {
  if (way === "change") {
    $.log.debug("工具模块已更新");
  }
});

API 参考

构造函数

new HotReload(config)

创建热重载实例。

  • config {Object} 配置参数
    • path {String} 监听的根路径,默认为模块所在目录
    • watch {Boolean} 是否开启监听,默认为true
    • unwatch {Boolean} 文件删除时是否自动取消监听,默认为false
    • ext {Array} 监听的文件扩展名,默认['.js', '.json']
    • dir {Array} 监听的目录模式,默认['/app/', '/com/', '/game/', '/demo/']
    • log_level {String} 日志级别,默认'error'
    • debounce_time {Number} 防抖时间(毫秒),默认0
    • watch_mode {String} 监听模式,默认'non-polling'

方法

load(file, func)

加载并监听模块文件。

  • file {String} 文件路径
  • func {Function} 文件变化时的回调函数
    • newModule {Object} 更新后的模块内容
    • way {String} 更新方式:'add'、'change'或'delete'
    • path {String} 文件路径
  • 返回:{Object} 加载的模块对象

unload(file, func)

卸载模块。

  • file {String} 文件路径
  • func {Function} 卸载完成的回调函数
    • path {String} 文件路径
  • 返回:{Boolean} 卸载是否成功

reload(file, func)

重新加载模块。

  • file {String} 文件路径
  • func {Function} 重载后的回调函数
  • 返回:{Object} 重新加载的模块对象

watch(path, func)

手动监听文件。

  • path {String} 文件路径
  • func {Function} 文件变化时的回调函数
  • 返回:{String} 监听的路径

close(func)

关闭监听器。

  • func {Function} 关闭后的回调函数

开发规范

  • 使用纯JavaScript开发,支持ES6+语法
  • 遵循严格的代码规范(ESLint + Prettier)
  • 所有公开方法都有完整的JSDoc注释
  • 完善的错误处理和参数校验
  • 方法长度不超过40行,确保代码可读性

测试

npm test

注意事项

  1. JSON文件的热重载是通过直接读取文件实现的,而JavaScript文件则是通过清除require缓存实现
  2. 当文件被删除时,如果配置了unwatch: true,将自动取消对该文件的监听
  3. 如果监听目录被删除,且配置了unwatch: true,将自动取消该目录下所有文件的监听
  4. 模块更新失败时会在控制台输出错误信息,但不会影响程序的继续运行

许可证

MIT