jaredpalmer / jaredpalmer/backpack

how can i do require() and dynamic require

Open
#108 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
4.4k
Forks
164
PR merge metrics
No merged PRs in 30d

Description

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 "."
```

Contributor guide

Open the contributing guide

Research direction

Start with the reported routersDir, fs.readdirSync, and dynamic require(filePath) example and reproduce the “Cannot find module” error. The issue names no repository file, test, or intended code change; done would require clarifying whether a bug fix, documentation update, or usage explanation is wanted.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
build-system, tooling
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
10/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.