Next.js Discord

Discord Forum

Turbopack dynamic import problem

Unanswered
Asian black bear posted this in #help-forum
Open in Discord
Asian black bearOP
Hey, I have a problem with importing files in for loop using Turbopack.
As suggested in the github issue template, it's better to clarify it before populating a new one.

Please consider this code:
const files = await fs.readdir(filesPath);
for (const file of files) {
   const newsletter = await import(`../newsletters/${file}`);
   console.log({name: newsletter.Name, path: `../newsletters/${file}`})
}

Essentially, it loads a module, checks forName and shows path and name;

So, with --turbo I get next output:
{ name: 'Launch 2.0', path: '../newsletters/launch2-0.tsx' }
{ name: 'Launch 2.0', path: '../newsletters/patch4.0.tsx' }
{ name: 'Launch 2.0', path: '../newsletters/patch4.1.tsx' }

And without I receive correct names
{ name: 'Launch 2.0', path: '../newsletters/launch2-0.tsx' }
{ name: 'Patch 4.0', path: '../newsletters/patch4.0.tsx' }
{ name: 'Patch 4.1', path: '../newsletters/patch4.1.tsx' }

So it seems to cache it or something?..

10 Replies

Create a issue on github. Turbo pack is still not stable and only available experimentally so these things can definitely happen.
Asian black bearOP
Okay, thanks!
Maybe also I can ask you about another inconsistent thing between turbopack and webpack?

I use App folder, and there is a page, that imports dynamically a module, using async import. With turbopack, changes in this module trigger hot-reloading of the page, but without don't.

export default async function Page ({params}: PreviewProps) {
  const {slug, project} = params;
  const { default: MyComponent, Name = null } = await import(`../${project}/${slug}`);
  return <MyComponent/>
}

It's a little simplified example, but I for some reason without --turbo I have to reload the page to see changes in MyComponent
(But I see that it was recompiled though)
What even is that
awaiting import inside a function definition? Maybe my typescript isnt good enough for this
Asian black bearOP
Yes, so server function (page) can be async and I await for the import. It works, but turbopack also triggers hot reloading, while webpack doesn't
Pages shouldn't be async tho
That seems like a page to me
I guess i was wrong