Export Files as Modules
export-files is a Node.js utility designed to automatically export all JavaScript files within a specified directory as modules. It provides a simple mechanism to aggregate multiple source files into a single object, where each file's export becomes a property. The current stable version is 3.0.2, with previous major breaking changes noted in v3.0.0 and v0.2.0. A key differentiator is its exclusive focus on CommonJS `require` syntax, explicitly stating it does not support ES Module `import` statements. It offers robust features like recursive directory processing, file filtering, and customizable key casing for the exported modules, making it suitable for organizing larger Node.js projects into modular structures without manual `require` statements for every file.
Common errors
-
SyntaxError: Cannot use import statement outside a module
cause Attempting to use ES Modules `import` syntax with the CommonJS-only `export-files` library.fixReplace `import exportFiles from 'export-files';` with `const exportFiles = require('export-files');` -
TypeError: require(...) is not a function
cause Incorrectly calling the `export-files` module without passing the required directory path as the first argument.fixEnsure you pass the directory path, typically `__dirname`, as the first argument: `require('export-files')(__dirname);` -
Error: Cannot find module 'my-non-javascript-file.json'
cause Attempting to export a non-JavaScript file, which `export-files` no longer supports since v0.2.0.fixEnsure that the directory only contains `.js` files you wish to export, or use the `to-exports` package for other file types.
Warnings
- breaking Version 3.0.0 introduced significant breaking changes related to improved support for recursion and renaming keys. Users upgrading from v2.x should review the README for updated options and behavior.
- breaking Version 0.2.0 removed support for non-JavaScript files. This library now exclusively handles `.js` files. Functionality for exporting other file types was moved to the `to-exports` package.
- gotcha This library is designed for CommonJS environments only and exclusively uses `require`. It does not support ES Modules (`import` statements). Attempting to use it with `import` will result in runtime errors.
- gotcha The default `.filter` option excludes files named `index.js`. If you intend to export an `index.js` file from your directory, you must explicitly override this filter.
Install
-
npm install export-files -
yarn add export-files -
pnpm add export-files
Imports
- exportFiles
import exportFiles from 'export-files';
const exportFiles = require('export-files'); - Directory Export
export default require('export-files')(__dirname);module.exports = require('export-files')(__dirname); - Export with Options
import { recursive } from 'export-files/options';const modules = require('export-files')(__dirname, null, { recursive: true, ignoreDirs: [] });
Quickstart
const path = require('path');
const fs = require('fs');
const exportFiles = require('export-files');
// 1. Create a dummy directory and files for demonstration
const exampleDir = path.join(__dirname, 'temp_example_modules');
const nestedDir = path.join(exampleDir, 'nested');
if (!fs.existsSync(exampleDir)) fs.mkdirSync(exampleDir, { recursive: true });
if (!fs.existsSync(nestedDir)) fs.mkdirSync(nestedDir, { recursive: true });
fs.writeFileSync(path.join(exampleDir, 'utilA.js'), `module.exports = { getA: () => 'Value from utilA' };`);
fs.writeFileSync(path.join(exampleDir, 'utilB.js'), `module.exports = { getB: (name) => \`Hello, \${name} from utilB\` };`);
fs.writeFileSync(path.nestedDir, 'utilC.js'), `module.exports = { getC: () => 'Value from nested utilC' };`);
// 2. Use export-files to load the modules
const modules = exportFiles(exampleDir, undefined, { recursive: true });
console.log('Exported module keys:', Object.keys(modules));
// Expected output: Exported module keys: [ 'utilA', 'utilB', 'utilC' ]
console.log('Call utilA.getA():', modules.utilA.getA());
// Expected output: Call utilA.getA(): Value from utilA
console.log('Call utilB.getB("World"):', modules.utilB.getB('World'));
// Expected output: Call utilB.getB("World"): Hello, World from utilB
console.log('Call utilC.getC():', modules.utilC.getC());
// Expected output: Call utilC.getC(): Value from nested utilC
// 3. Clean up the dummy directory
process.on('exit', () => {
if (fs.existsSync(exampleDir)) {
fs.rmSync(exampleDir, { recursive: true, force: true });
console.log('Cleaned up temp_example_modules directory.');
}
});