backpack
backpack copied to clipboard
how can i do require() and dynamic require
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 "."
does this code work in the default node.js?