backpack icon indicating copy to clipboard operation
backpack copied to clipboard

how can i do require() and dynamic require

Open ezioruan opened this issue 7 years ago • 1 comments

i have the following code

const routersDir = path.resolve(path.join(__dirname, 'routers'))

const getRouters = () =>
  fs.readdirSync(routersDir).forEach(file => {
    if (path.extname(file) === '.js') {
      const filePath = path.join(routersDir, file);
      console.log("filePath", filePath)
      const router = require(filePath)
      console.log("router", router)
    }
  })

i want to load the module in a dir but i got error

 const router = require(filePath)
                     ^
Error: Cannot find module "."

ezioruan avatar Jan 30 '18 06:01 ezioruan

does this code work in the default node.js?

dsseng avatar Feb 11 '18 12:02 dsseng