Module management system for Node.js that provides dynamic loading, unloading, and hot-reloading capabilities for your application modules.
- 🔄 Dynamic module loading and unloading
- 🔍 Automatic dependency tracking
- 🔥 Hot-reloading support
- 📦 Module lifecycle management
- 🧩 Context-aware module initialization
- 📡 Event emitter capabilities for inter-module communication
- ⏱️ Automatic timeout and interval management
- 🧹 Automatic cleanup on process exit and error handling
npm install modsconstmods=require('mods');// Initialize with contextconstctx={// Your application context};// Autoload all modules from the mods directorymods.autoload(ctx);// Load a single moduleconstmodule=awaitmods.load('moduleName',ctx);// Load multiple modulesconstmodules=awaitmods.load(['module1','module2'],ctx);// Unload a single moduleawaitmods.unload('moduleName');// Unload multiple modulesawaitmods.unload(['module1','module2']);// Unload all modulesawaitmods.unload();// Reload a single moduleawaitmods.reload('moduleName',ctx);// Reload multiple modulesawaitmods.reload(['module1','module2'],ctx);// Reload all loaded modulesawaitmods.reload(null,ctx);Each module is an EventEmitter instance, so you call use it for exchanging events between modules. All event listeners will be removed automatically when module unloads.
// Inside a modulethis.emit('eventName',data);// Listen for eventsthis.on('eventName',(data)=>{// Handle event});// Broadcast events to all modulesthis.broadcast('eventName',data);Alternatively, you can directly call methods on other modules:
this.mods.loaded.otherModule.someMethod(data);The only thing to remember here: you should never store a reference to modules (this.mods.loaded.otherModule in this example), because they will become stale after reload.
There are also wrappers for setTimeout and setInterval functions which are automatically cleared on module unload.
// Set a timeout that will be automatically cleared on unloadthis.setTimeout(()=>{// Your code},1000);// Set an interval that will be automatically cleared on unloadthis.setInterval(()=>{// Your code},1000);Modules should be placed in the mods directory and follow this structure:
// mods/example.jsmodule.exports=asyncfunction(ctx){// Module initialization code, can be asyncthis.someMethod=()=>{// You can declare methods here to make them available from other modules}// Return cleanup functionreturnasync()=>{// Cleanup code};};- Module Loading: When a module is loaded, it receives a context object and returns a cleanup function.
- Dependency Tracking: The system automatically tracks module dependencies through a graph system.
- Event System: Modules can communicate through events using the built-in event emitter.
- Resource Management: Timeouts and intervals are automatically managed and cleaned up.
- Hot Reloading: Modules can be reloaded without restarting the application.
- Cleanup: When unloading, the cleanup function is called to properly dispose of resources.
- Process Management: Automatic cleanup on process exit and error handling.
MIT