import-all-index

2.0.3 • Public • Published

import-all-index

Uses dynamic import() to recursively loop through a directory tree, import all index.js or index.mjs files and return an array of references to the imported modules

Requires esm package or Node 12.

Usage:

import path from 'path'
import importAllIndex from 'import-all-index'
 
export default async server => {
  let absolutePath = import.meta.url.replace('file://', '')
  let apiRoutes = await importAllIndex(path.resolve(absolutePath, '../folder_to_import'))
 
  apiRoutes.forEach(importedModule => {
    if (importedModule.default && typeof importedModule.default === 'function') {
      importedModule.default(server) // runs all imported defaults that are functions injecting the server object provided to the parent function
    }
  })
}
 

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 2.0.3
    212
    • latest

Version History

Package Sidebar

Install

npm i import-all-index

Weekly Downloads

216

Version

2.0.3

License

MIT

Unpacked Size

3.67 kB

Total Files

4

Last publish

Collaborators

  • lorengreenfield