{"id":15107,"library":"export-files","title":"Export Files as Modules","description":"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.","status":"active","version":"3.0.2","language":"javascript","source_language":"en","source_url":"https://github.com/jonschlinkert/export-files","tags":["javascript","export","exports","file","files","module","modules"],"install":[{"cmd":"npm install export-files","lang":"bash","label":"npm"},{"cmd":"yarn add export-files","lang":"bash","label":"yarn"},{"cmd":"pnpm add export-files","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This library is CommonJS-only and explicitly does not support ES Modules (import/export syntax).","wrong":"import exportFiles from 'export-files';","symbol":"exportFiles","correct":"const exportFiles = require('export-files');"},{"note":"The primary use case is to export an entire directory's contents using Node.js's `module.exports`.","wrong":"export default require('export-files')(__dirname);","symbol":"Directory Export","correct":"module.exports = require('export-files')(__dirname);"},{"note":"Options are passed as the third argument to the main function, not imported separately.","wrong":"import { recursive } from 'export-files/options';","symbol":"Export with Options","correct":"const modules = require('export-files')(__dirname, null, { recursive: true, ignoreDirs: [] });"}],"quickstart":{"code":"const path = require('path');\nconst fs = require('fs');\nconst exportFiles = require('export-files');\n\n// 1. Create a dummy directory and files for demonstration\nconst exampleDir = path.join(__dirname, 'temp_example_modules');\nconst nestedDir = path.join(exampleDir, 'nested');\n\nif (!fs.existsSync(exampleDir)) fs.mkdirSync(exampleDir, { recursive: true });\nif (!fs.existsSync(nestedDir)) fs.mkdirSync(nestedDir, { recursive: true });\n\nfs.writeFileSync(path.join(exampleDir, 'utilA.js'), `module.exports = { getA: () => 'Value from utilA' };`);\nfs.writeFileSync(path.join(exampleDir, 'utilB.js'), `module.exports = { getB: (name) => \\`Hello, \\${name} from utilB\\` };`);\nfs.writeFileSync(path.nestedDir, 'utilC.js'), `module.exports = { getC: () => 'Value from nested utilC' };`);\n\n// 2. Use export-files to load the modules\nconst modules = exportFiles(exampleDir, undefined, { recursive: true });\n\nconsole.log('Exported module keys:', Object.keys(modules));\n// Expected output: Exported module keys: [ 'utilA', 'utilB', 'utilC' ]\n\nconsole.log('Call utilA.getA():', modules.utilA.getA());\n// Expected output: Call utilA.getA(): Value from utilA\n\nconsole.log('Call utilB.getB(\"World\"):', modules.utilB.getB('World'));\n// Expected output: Call utilB.getB(\"World\"): Hello, World from utilB\n\nconsole.log('Call utilC.getC():', modules.utilC.getC());\n// Expected output: Call utilC.getC(): Value from nested utilC\n\n// 3. Clean up the dummy directory\nprocess.on('exit', () => {\n  if (fs.existsSync(exampleDir)) {\n    fs.rmSync(exampleDir, { recursive: true, force: true });\n    console.log('Cleaned up temp_example_modules directory.');\n  }\n});","lang":"javascript","description":"Demonstrates how to use `export-files` to load JavaScript modules from a directory, including nested directories, and access their exports. The example sets up a temporary directory with modules and cleans up afterwards."},"warnings":[{"fix":"Review the official README for v3.0.0 to understand the new `recursive` and `.case` options, and adjust your usage accordingly.","message":"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.","severity":"breaking","affected_versions":"3.0.0"},{"fix":"Ensure you are only attempting to export `.js` files. For non-JavaScript files, consider using the `to-exports` package.","message":"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.","severity":"breaking","affected_versions":"0.2.0"},{"fix":"Always use `const module = require('export-files');` syntax. If your project uses ES Modules, you may need to wrap `export-files` in a CommonJS module or re-evaluate your module loading strategy.","message":"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.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Pass a custom `filter` function to the options, e.g., `{ filter: file => true }` or `{ filter: file => file.name !== 'another-file.js' }`.","message":"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.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Replace `import exportFiles from 'export-files';` with `const exportFiles = require('export-files');`","cause":"Attempting to use ES Modules `import` syntax with the CommonJS-only `export-files` library.","error":"SyntaxError: Cannot use import statement outside a module"},{"fix":"Ensure you pass the directory path, typically `__dirname`, as the first argument: `require('export-files')(__dirname);`","cause":"Incorrectly calling the `export-files` module without passing the required directory path as the first argument.","error":"TypeError: require(...) is not a function"},{"fix":"Ensure that the directory only contains `.js` files you wish to export, or use the `to-exports` package for other file types.","cause":"Attempting to export a non-JavaScript file, which `export-files` no longer supports since v0.2.0.","error":"Error: Cannot find module 'my-non-javascript-file.json'"}],"ecosystem":"npm"}