node-require-directory icon indicating copy to clipboard operation
node-require-directory copied to clipboard

ECMAScript modules example

Open brandonros opened this issue 5 years ago • 3 comments

Might make sense to put one in the README?

brandonros avatar Dec 11 '19 01:12 brandonros

Does this package work with the Node 14 ESM implementation at all?

jorgegonzalez avatar Apr 30 '20 15:04 jorgegonzalez

export const initRoutes = async (app) => {
  const __filename = url.fileURLToPath(import.meta.url)
  const __dirname = path.dirname(__filename)
  const filenames = glob.sync(path.resolve(__dirname + '/../routes/**/*.mjs'))
  for (let i = 0; i < filenames.length; ++i) {
    const filename = filenames[i]
    const { default: route } = await import(filename)
    app[route.method](
      route.path,
      buildRequestIdMiddleware(),
      buildRequestLoggerMiddleware(),
      buildRateLimitMiddleware(route),
      buildAsyncRouteHandlerMiddleware(route),
      buildResponseLoggerMiddleware()
    )
  }
}

probably not. i abandoned trying to use it it and did this.

brandonros avatar Apr 30 '20 17:04 brandonros

I came across this issue today where I wanted to use ES6 imports which this package doesn't support. My use case was initializing routes/middlewares on app start. So I made a simple alternative that lets me use the visit and include options like this package but with ESM. Works well with top-level await. Here's an example

mohit-s96 avatar Feb 22 '22 06:02 mohit-s96