Next.js Discord

Discord Forum

Dynamic import importing wrong file when using Turbopack

Answered
Spinge Bib Sqorpnts posted this in #help-forum
Open in Discord
Avatar
I am attempting to dynamically import a json file inside a useEffect, and it works fine when not using turbopack, but when I add the --turbo flag, the import statements imports the FIRST file in the directory, instead of the one I need.

Snippet:
locale is a state object that is initialized to he and is not changed, yet it imports the first file in the translations dir, which is ar.json. Hard-coding the path to end with he.json imports the correct file.
import(`../public/assets/translations/${locale}.json`)
  .then((msgs) => 
  {
    console.log(msgs.default); // Checking which file was imported
    setMessages(msgs.default);
  })
  .catch((e) => console.error('Failed to load messages:', e));
Answered by Spinge Bib Sqorpnts
Seems to be fixed in canary, reproduction env:

https://xpp7p5-3000.csb.app/
View full answer

4 Replies

Avatar
I'm using the latest version of next, will attempt next canary in a few moments
Avatar
Seems to be fixed in canary, reproduction env:

https://xpp7p5-3000.csb.app/
Answer
Avatar
Unfortunately it seems that at least for windows, Turbopack is borked atm: https://github.com/vercel/next.js/issues/63359
Avatar
yeah turbopack is still quite buggy. my general rule of thumb is that, if you do something and it works in webpack but doesn't work in turbopack, it's turbopack's fault not yours and you are forced to use webpack in that case