requireindex
Generates a Node.js index.js that auto-requires and exports sibling files and directories by basename.
Repository Health
Technical Analysis
requireindex is a tiny Node.js utility that eliminates the boilerplate of manually writing barrel index.js files. Instead of hand-listing a require() call and export for every sibling module in a directory, you call requireindex(__dirname) from an index.js; it synchronously reads the directory, requires each .js, .node, and .json file it finds (skipping index.js itself and anything prefixed with _ or .), and returns a single object keyed by file basename.
An optional second argument — an array of basenames — restricts the export to only those named files rather than everything in the directory. This makes the library useful for aggregating a folder of plugins, CLI subcommands, routes, or utility modules into one exported object without maintaining that list by hand.
What You Get
- Automatic directory-to-object export via synchronous readdirSync and require calls
- Underscore- and dot-prefixed file exclusion for marking modules private
- Optional explicit basename allowlist for selective exports instead of requiring everything
- Lowercase-alpha file sorting for consistent export ordering across filesystems
Common Use Cases
- Building a directory of CLI subcommands that auto-register without a manual index
- Aggregating a folder of utility functions into one importable namespace
- Assembling route or model files in a Node.js app into a single exported object
- Creating a plugin directory where each file self-registers by filename
Under The Hood
Architecture
The entire library is a single exported function in index.js: it branches on arguments.length to either require an explicit list of basenames or fall back to scanning the target directory with fs.readdirSync, filtering out index.js and any name starting with _ or ., then require()-ing each remaining .js, .node, or .json entry and assigning it onto a plain object keyed by basename. There is no internal layering beyond this one function — the whole contract is “call it with a directory, get an object back” — which keeps the surface area and the failure modes easy to reason about, since anything that goes wrong reduces to a single readdirSync/require call site.
Tech Stack
The runtime code has zero dependencies and uses only Node’s built-in fs and path modules, targeting node >= 0.10.5 per package.json’s engines field. Tests run via a single devDependencies entry (asserts) invoked through node test/test.js, and .travis.yml wires up Travis CI against an old Node 0.10 runtime — there is no modern build step, bundler, or transpiler in the project.
Code Quality
A single test/test.js file exercises the public behavior against a fixture tree under test/lib/ — nested directories, underscore- and dot-prefixed private files, a non-JavaScript file, and the explicit-basenames form — using the asserts/assert combination rather than a contemporary test runner. There is no TypeScript, no linter configuration, and no type declarations anywhere in the repo; error handling is limited to a single explicit throw new Error(...) when no directory argument is supplied at all.
API Design
The public API is deliberately minimal: one function, one required argument, one optional array argument, called once from an index.js you write yourself. That minimalism keeps adoption to a single line of boilerplate, but the underlying pattern — deriving an export object from directory contents — is common to several comparable Node packages, so the design is convenient rather than novel.