Next.js Discord

Discord Forum

Unable to load module dynamically, if the module name is dynamic

Unanswered
Australian Freshwater Crocodile posted this in #help-forum
Open in Discord
Australian Freshwater CrocodileOP
This works

  await import("src/records/flights/2020/20200915EDKBEDKB").then(mod => mod.flight);


but this does not :

  const readPromises = years.map((f) => fs.readdir(f, { withFileTypes: true }));
  const flightPromises = (await Promise.all(readPromises))
    .flat()
    .filter((e) => e.isFile() && e.name.includes(".ts"))
    .map((e) => `${e.path.replace("./", "")}/${e.name.replace(".ts", "")}`)
    .map((e) => import(e).then((mod) => mod.flight));


Error:

Error: Cannot find module 'src/records/flights/2020/20200915EDKBEDKB'


This is being done as part of a RSC.

const StatsPage = async () => {
  const { flightLog } = await getFlightLog();

  return <></>
} 

export default StatsPage;

0 Replies