Registry / serialization / requireindex

requireindex

JSON →
library1.2.0jsnpmunverified

requireindex is a utility for Node.js CommonJS environments that simplifies the creation of `index.js` files by automatically requiring and exporting sibling modules within a directory. It aggregates modules into a single object, with keys corresponding to file basenames. It supports nested directories, allows explicit file inclusion, and respects a convention for private modules (prefixing with an underscore). The current stable version is 1.2.0, which was last published over 8 years ago, indicating the package is no longer actively maintained. It offers a concise alternative to manually writing multiple `require()` statements for structuring CommonJS projects, though its relevance has diminished with the widespread adoption of ES Modules.

serialization
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

This package is strictly CommonJS. Attempting to use `import` will result in a runtime error.

const requireIndex = require('requireindex');

The package's core function is intended to be called with `__dirname` to process the current directory and its modules for `module.exports`.

module.exports = require('requireindex')(__dirname);

An optional second argument allows specifying an array of basenames to explicitly include, ignoring other files in the directory.

module.exports = require('requireindex')(__dirname, ['file1', 'file2']);

Demonstrates how to use `requireindex` to automatically load and export modules from a directory, including showing how it ignores files prefixed with an underscore.

const fs = require('fs'); const path = require('path'); // Create a temporary directory structure for demonstration const tempDir = path.join(__dirname, 'temp_modules'); fs.mkdirSync(tempDir, { recursive: true }); fs.writeFileSync(path.join(tempDir, 'moduleA.js'), 'module.exports = { a: 1 };'); fs.writeFileSync(path.join(tempDir, 'moduleB.js'), 'module.exports = { b: 2 };'); fs.writeFileSync(path.join(tempDir, '_privateModule.js'), 'module.exports = { private: true };'); // Emulate index.js in temp_modules const requireIndex = require('requireindex'); const modules = requireIndex(tempDir); console.log('Discovered modules:', modules); // Expected output: { moduleA: { a: 1 }, moduleB: { b: 2 } } // Clean up temporary directory fs.unlinkSync(path.join(tempDir, 'moduleA.js')); fs.unlinkSync(path.join(tempDir, 'moduleB.js')); fs.unlinkSync(path.join(tempDir, '_privateModule.js')); fs.rmdirSync(tempDir);
Debug
Known footguns
breakingThis package is designed exclusively for CommonJS (CJS) environments in Node.js. It does not support ES Modules (ESM). Attempting to `import` this package or use it within an ESM project will cause runtime errors like `ReferenceError: require is not defined`.
gotchaThe package has not been updated in over 8 years. While functional for its specific use case, it is considered abandoned. This means no new features, bug fixes, or security patches will be released. Use with caution in critical applications.
gotchaThere are no official TypeScript declarations provided by the package itself. Community-maintained types (`@types/requireindex`) exist but may not always be up-to-date or fully accurate with the specific behavior of the package.
Upgrade
Version history

Breaking-change detection hasn't run for this library yet.

Audit
Security & dependencies

CVE tracking and dependency tree are planned for a later release.

Agent activity
0 hits · last 30 days

No traffic data recorded yet.

Resources