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

mod-loader

v0.0.1

Published

Light module loader based on nodejs fs, uses async and sync mode

Downloads

12

Readme

mod-loader

Lite synchronous and asynchronous module loader to make a dynamic use of require on a specified directory.

Installation


  npm install mod-loader --save

General

mod-loader is a node module that can be used to easyly include your own code digging directories using native node fs module.

Usage

mod-loader has to basic methods

loadModulesSync: ({ baseDirectory = null, moduleHolder = null, doNotInclude = [], namingStrategy = defaultNamingStrategy },loaderFunction)

Load modules in a synchronous way, this method receives two params

  • options: Options to perform the module loading;

    • baseDirectory(String): Base directory where all the loading will begin
    • moduleHolder(Object): Target Object, this object will receive every require(..) as a property according to the naming strategy
    • doNotInclude(Array): An array of files or paths to be excluded
    • namingStrategy(Function): A function used to name each property in moduleHolder object, the namingStrategy is a function which receives the loaded module full path, and the module itself. Inside this function you will have the freedom to determine which name the module will have over the holder, by default the naming strategy will use the file's name, for example Category.js will become Category, however you can implement whatever strategy is suitable by your use case.
  • loaderFunction: loaderFunction is the function used to involve require(..) calls you must return a require(module..) with your specific constructors for the module.

loadModulesSync: ({ baseDirectory = null, moduleHolder = null, doNotInclude = [], namingStrategy = defaultNamingStrategy },loaderFunction,loadDone)

Load modules in a asynchronous way, this method receives three params

  • options: Options to perform the module loading;

    • baseDirectory(String): Base directory where all the loading will begin require(..) as a property according to the naming strategy
    • doNotInclude(Array): An array of files or paths to be excluded
    • namingStrategy(Function): A function used to name each property in moduleHolder object, the namingStrategy is a function which receives the loaded module full path, and the module itself. Inside this function you will have the freedom to determine which name the module will have over the holder, by default the naming strategy will use the file's name, for example Category.js will become Category, however you can implement whatever strategy is suitable by your use case.
  • loaderFunction: loaderFunction is the function used to involve require(..) calls you must return a require(module..) with your specific constructors for the module.

  • loadDone: Callback function which marks the loading process as done, it will receive as a parameter the object holder, in this case the holder is provided by the function itself.

Use cases

You can ask yourself, why would I need a f**ng module loader? here are some useful use cases

Sequelize's ORM boilerplate index

The main create this module was to reduce the boilerplate used to load sequelize's ORM models into a single db property, the example is as follows:

"use strict"

module.exports = function() {
  const moduleLoader = require("mod-loader");
  const Sequelize = require("sequelize");
  const sequelize = new Sequelize(...);

  const path = require("path");

  let db = {};

  moduleLoader.loadModulesSync({
    baseDirectory: path.join(__dirname),//Path to start lstat(current directory)
    moduleHolder: db,//Holder which will receive require(..)
    doNotInclude: [ //array of directories, or files to exclude
      "index.js"//exclude self to avoid maximum stack call exceed :)
    ]
  },(moduleLoaded) => {
    //According to sequelize's page you have define your modules as self
    //contained functions which receive sequelize connection and base sequelize as //parameter
    return require(moduleLoaded)(sequelize,Sequelize);
  });

  Object.keys(db).forEach((modelName) => {
    if ("associate" in db[modelName]) {
      db[modelName].associate(db);
    }
  });

  db.sequelize = sequelize;
  db.Sequelize = Sequelize;

  return db;
}

Express's middleware grouping

When developing express's applications is almost an imperative requirement to develop middlewares, those fancy functions which executes before, or after your http call. In some cases express applications may have a lot of middleware to be used so may come in handy to have every middleware available.


const moduleLoader = require("mod-loader");
const path = require("path");

let middleware = {};

moduleLoader.loadModulesSync({
  baseDirectory: path.join(__dirname),//Path to start lstat(current directory)
  moduleHolder: middleware,//Holder which will receive require(..)
  doNotInclude: [ //array of directories, or files to exclude
    "index.js"//exclude self to avoid maximum stack call exceed :)
  ]
},(moduleLoaded) => {
  return require(moduleLoaded)();
});

module.exports =  middleware;
.
.
//in your further script you can use the middleware by just requiring the directory's index
const middleware = require("./middleware");

console.log(middleware);//this object will hold all your middleware

Everything wich requires to collect a group of modules into a "holder"

If you found yourself in the need to use an object which collects modules from a directory, then mod-loader may be suitable for your needs, it does not require any additional node modules and uses native node's fs module

Tests

  npm test