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

node-modules-manager

v1.6.0

Published

A simple module loader and manager to optimize lazy loading.

Downloads

4

Readme

node-modules-manager

A lightweight module loader and manager to keep your modules organised and help with lazy-loading.

How to use

Set up a base directory

The module loader requires a base path to be able to handle relative module paths. You can pass the base path as the first parameter of the constructor.

const Manager = require('node-modules-manager').createManager('full/path/to/rootdir');

Loading modules

Load locally installed modules

To load a module installed globally or as dependency, you can simply pass the module's name to the constructor loadModule() method to load the module for later use, or the getModule() method to use the module right away. You can also pass an object to customize the name you access your modules by.

const Manager = require('node-modules-manager').createManager('rootdir');

// To load a module for later use. Returns true upon success or false if the module wasn't found.
Manager.loadModule('installedModule');

// To use a module and load it to the modules list. Returns the loaded module for immediate use.
Manager.getModule('fs').readDirSync(/* ... */);

Load modules by path

To load a module by path, you can pass the module's path to the constructor, loadModule() or getModule(). The module will then be accessible through either its full path or the filename of the module (the part of the path after the last /). You can also pass an object to customize the name you access your modules by.

const Manager = require('node-modules-manager').createManager('rootdir');

// Load module by passing full path
Manager.loadModule('path/to/module.js');

// Can later be accessed by calling:
Manager.getModule('path/to/module.js');

//OR
Manager.getModule('module.js');

Initialize with preloaded modules

Sometimes there are some modules you simply cannot delay loading. To avoid making unnecessary calls to loadModule(), you can pass an array of module names, paths, or objects to the constructor and the module loader will load them right away.

const Manager = require('node-modules-manager').createManager(
														'rootdir',
														[
															'fs', 
															'path', 
															'path/to/some/module', 
															'other-required-module'
														]
													);

Load modules with an alias

To access your modules by a name different from the module's own name, you can pass an object to the constructor or loadModule() methods. The keys name and path must be specified on all objects passed this way, and can later be accessed by passing the value of name to getModule().

// Initialize and pass aliased object as module to the constructor
const Manager = require('node-modules-manager').createManager(
														'rootdir',
														[
															{
																name: 'exampleModule', 
																path:'../path/to/example-module.min.js'
															}
														]
													); // will preload the contents of 'rootdir/path/to/example-module.min.js' as 'exampleModule'

// OR
Manager.loadModule({name: 'exampleModule', path:'../path/to/example-module.min.js'}); // will preload the contents of 'rootdir/path/to/example-module.min.js' as 'exampleModule'

// OR 
Manager.getModule({name: 'exampleModule', path:'../path/to/example-module.min.js'}); // will preload as 'exampleModule' and return the contents of 'rootdir/path/to/example-module.min.js'

// Then sometime later...
Manager.getModule('exampleModule'); //will return the contents of 'rootdir/path/to/example-module.min.js'

Loading a module once

Do you only need to call a module once and then remove all references to it to keep it from hogging memory? This project has you covered! You can simply pass a callback along with the name of the module it requires to ˙getModuleOnce()` and be rid of all traces outside of your callback function. If the module has been preloaded beforehand and you want to unload it after use, set the optional third parameter of the method to a truthy value - this will force unload the module after the callback has executed. This method will warn you if you already preloaded the required module to highlight possible optimization issues.

const Manager = require('node-modules-manager').createManager('rootdir');

Manager.getModuleOnce('exampleModule', function(module) {
	console.log(module); //exampleModule
});

// the module never lives outside the Manager

// Or set the forceUnload parameter to true to remove it from the list of loaded modules after use
Manager.loadModule('exampleModule');
Manager.getModuleOnce('exampleModule', function(module) {
	console.log(module);//exampleModule
}, true);

console.log(Manager.isModuleLoaded('exampleModule')); //false

Unloading modules

When you're done using some module functionality, it is sometimes preferable to drop the module from memory instead of bloating your Manager instance if you don't plan on reusing it soon. You can unload the contents of a module at any time by calling the unloadModule() method with either a module name, path or alias. The method returns null on success so that its return value can be used to reset your references to the module.

// Load the module
const Manager = require('node-modules-manager').createManager('rootdir');
let someModule = Manager.loadModule('someModule');

// Use it while it's hot
someModule.doSomething();

// Drop from memory to speed things up when you're done
someModule = Manager.unloadModule('someModule');

console.log(someModule); // null