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.

npm install requireindex
INSTALL
IMPORT
SIG · REQUIREINDEX
R
requireindex
serializationjavascriptv1.2.0
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

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

requireindex function
✓ const requireIndex = require('requireindex');
✗ import requireIndex from 'requireindex';
This package is strictly CommonJS. Attempting to use `import` will result in a runtime error.
Module export pattern
✓ module.exports = require('requireindex')(__dirname);
✗ export default require('requireindex')(__dirname);
The package's core function is intended to be called with `__dirname` to process the current directory and its modules for `module.exports`.
Specific file inclusion
✓ module.exports = require('requireindex')(__dirname, ['file1', 'file2']);
An optional second argument allows specifying an array of basenames to explicitly include, ignoring other files in the directory.

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 issues
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`.
fix
Ensure your project is configured for CommonJS, or use a transpiler like Babel to convert your code to CJS if you must use `requireindex` in a mixed environment. For new projects, consider native ESM solutions or other module loading patterns.
affects: >=1.0.0
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.
fix
Evaluate modern alternatives for module aggregation (e.g., using `fs` and dynamic `import()`/`require()` with ESM-compatible loaders) if long-term maintenance and security are concerns.
affects: >=1.0.0
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.
fix
Install `@types/requireindex` if using TypeScript (`npm install --save-dev @types/requireindex`). Verify the types align with the package's actual runtime behavior, especially for edge cases.
affects: >=1.0.0
Errors
Common errors & fixes
ReferenceError: require is not defined
Attempting to use `requireindex` in an ES Module (ESM) context where the `require` function is not globally available.
fix
Ensure your Node.js project is running in CommonJS mode (e.g., by omitting `"type": "module"` in `package.json` or by using `.cjs` file extensions for CJS files). If you need ESM, you might need to reconsider using `requireindex`.
TypeError: requireindex is not a function
The result of `require('requireindex')` was called directly without passing the `__dirname` argument, or it was incorrectly assumed to be a direct object export rather than a function.
fix
Always call the result of `require('requireindex')` as a function, typically with `__dirname` as its first argument, like `require('requireindex')(__dirname)`.
Error: Cannot find module 'requireindex'
The `requireindex` package has not been installed or is not resolvable in the current Node.js environment.
fix
Run `npm install requireindex` in your project directory to ensure the package is correctly installed and available in `node_modules`.
Upgrade
Version history
1.2.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
Resources
requireindex — npm install requireindex · libregistry