I am writing a typescript + nodejs + express CLI for quick deploy of mockable servers
it dynamically imports mock server configurations from js files with router configurations in a given source dir
It currently works by using absolute paths
fs.readdirSync(routerSrc).filter((f: string) => /^.*\.js$/.test(f)).forEach((file: string) => {
const src = routerSrc + file;
import(src)
.catch(reason => logger.E("failed to load module", reason, {src, cwd: process.cwd()}))
.then(({routes, router}: RouterModule) => {
// apply routing
});
});
routerSrc
is passed as cli parameter. But passing in relative paths it fails to find the module
# this doesn't work
$ mock-server -r ./routers/
# this works fine
$ mock-server -r $PWD/routers/
I have read some lengthy answers (1, 2) on the subject, but I was unable to get it to work with these instructions
How can I get it to work with relative path imports? What am I missing?