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

modload

v1.1.0

Published

A module autoloader

Downloads

4

Readme

Node Autoloader

A simple autoloader for node modules.

Install

Local:

npm install modload --save

Global:

npm install modload -g

Examples

  • Require all files in a given directory and store the results in an array:

    const load = require('modload')
    
    const plugins = load.asArray({
      dir: path.join(__dirname, 'plugins')
    })
    
    server.register(plugins, (err) => {
      if (err) throw new Error(err)
    })
  • Require all files in a given directory and store the results in an object whose structure matches the directory structure:

    const load = require('modload')
    
    const app = load.asObject({
      dir: path.join(__dirname, 'app')
    })

    If the directory structure looks like:

    ./
    |- app
    |- |- routes
    |- |- |- home.js
    |- |- |- login.js
    |- |- plugins
    |- |- |- auth.js

    Then the resulting object will match this structure:

    const app = {
      routes: {
        home: require('./app/routes/home.js'),
        login: require('./app/routes/login.js')
      },
      plugins: {
        auth: require('./app/routes/auth.js')
      }
    }

    This object can be attached to the global object via an optional argument.

API

Modload provides two methods, which both take an options object to configure their behaviour.

asArray(options)

Return

Array of required modules.

Parameters
options

Options object with the following fields:

dir
( String | required )

Root directory of files to require

include
( Array of RegExp | default: null )

RegExp patterns of files to include. Patterns are combined into a single regex pattern with OR. Exclude patterns take precedence over include patterns.

exclude
( Array of RegExp | default: null )

RegExp patterns of files to exclude. Patterns are combined into a single regex pattern with OR. Exclude patterns take precedence over include patterns.

stopfile
( String or RegExp | default: null )

A file in the directory tree matching this pattern is interpreted as the entry point for it's parent directory. Only this file is required; all other files in the same directory, and in sub-directories, are skipped.

es6modules
( Boolean | default: false )

A flag to specify whether modules are exported with ES6 module syntax (i.e. using the export keyword). If true, modload will require the default value (i.e., whatever is exported by export default)

modules
( Array of Strings | default: null )

Array of module names to load. Useful for loading third-party modules at the same time as your own modules. Loaded before custom modules. For example load.asArray({... modules: ['hapi']}) will load the hapi module before any others specified.

asObject(options)

Return

Object of module objects, structured to reflect directory structure.

Parameters
options

Options object with exactly the same fields as for the asArray method, but with the following additional fields:

isglobal
( Boolean | default: false )

If true, the returned object will also be attached to the global object under the key specified by the namespace parameter

namespace
( String | default: 'app' )

Specifies the key of the global object to which the object will be attached.