question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

./i18n.js Critical dependency: the request of a dependency is an expression

See original GitHub issue

Today after doing some package updates e.g. next@12.1.6 next-translate crashes while trying to load the locale. Im importing my locales from a package inside my monorepo with:

./i18n.js

loadLocaleFrom: (locale, namespace) =>
    import(`locales/${locale}/${namespace}`).then((m) => m.default),

That worked for me for like 2 months now. But now i get following error:

./i18n.js
Critical dependency: the request of a dependency is an expression

After a while of trial and error i got my code working with:

  loadLocaleFrom: (locale, namespace) =>
    require(`locales/${locale}/${namespace}`),

Is the error here with me or is there a package error?

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:28
  • Comments:29 (10 by maintainers)

github_iconTop GitHub Comments

14reactions
aralrocacommented, Jul 4, 2022

Of course, it is not the right solution, but until we find the best way to fix it (all suggestions are welcome), you can do a workaround and not use the loadLocaleFrom inside i18n.js and overwrite the default:

const workaround = require('next-translate/lib/cjs/plugin/utils.js')

// As a workaround you can change the string of the defaultLoader, this is working fine
workaround.defaultLoader = '(l, n) => import(`@next-translate-root/src/locales/${l}/${n}.json`).then(m => m.default)';

module.exports = {
  ...config,
  // loadLocaleFrom: (lang, ns) =>
  //   import(`./src/locales/${lang}/${ns}.json`).then((m) => m.default),
}

Any suggestions from anyone on how to fix it internally in the library? Thanks!

10reactions
derkoecommented, May 10, 2022

We have the same problem and also fixed it with require (like https://github.com/vinissimus/next-translate/issues/851#issuecomment-1116090210).

We also had to catch the error on the require because not all of the files exist (we implement a fallback logic for country-specific locales like “es-CL” and then fallback to English):

  const englishTexts = require('./locales/en/myapp.json');
  ...
  loadLocaleFrom: (locale, ns) => {
    let countrySpecific = {},
      languageOnly = {};
    try {
      countrySpecific = require(`locales/${locale.replace('-', '_')}/${ns}.json`);
    } catch (error) {}
    try {
      languageOnly = require(`./locales/${locale.substring(0, 2)}/${ns}.json`);
    } catch (error) {}
    return _.merge({}, englishTexts, languageOnly, countrySpecific);
  },
Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues - GitHub
./i18n.js Critical dependency: the request of a dependency is an expression #851. Open.
Read more >
Critical dependency: the request of a dependency is an ...
I think problem is with my configuration. next.config.js const {i18n} = require('./next-i18next.config ...
Read more >
the request of a dependency is an expression nextjs - You.com
I am getting three warning messages when importing request in a barebone webpack project. A minimal example to reproduce the bug is available...
Read more >
kglogins - Netlify Support Forums
i18n, }. After running. npm run build. I get this error: next build ... Critical dependency: the request of a dependency is an...
Read more >
lingui/js-lingui - Gitter
... Critical dependency: the request of a dependency is an expression ../node_modules/@lingui/macro/node_modules/babel-plugin-macros/dist/index.js 62:46-53 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found