next-translate
next-translate copied to clipboard
The esm entry of next-translate-plugin does not use esm syntax
What version of this package are you using? [email protected]
What operating system, Node.js, and npm version? MacOS, node 19.8.1, npm 9.5.1
What happened? https://github.com/aralroca/next-translate-plugin/blob/main/src/index.ts#L39 Not able to use this plugin if my next config is a .mjs file.
What did you expect to happen?
// next.config.mjs
import withNextTranslatePlugin from 'next-translate-plugin'
const myConfig = { ... }
export default withNextTranslatePlugin(myConfig);
Are you willing to submit a pull request to fix this bug? Not sure how to fix this.
I will take a look how to fix it. Thanks to report it
Can you create a repo to reproduce it? I try it and this works for me:
next.config.mjs
import nextTranslate from "next-translate-plugin";
export default nextTranslate();
I might share some details on the problem. Let's assume we have a type: "module"
in our package.json.
- if we have the i18n.js file with
module.exports
, it fails because i18n.js isn't in esm syntax:
[build:next] - error Failed to load next.config.mjs, see more info here https://nextjs.org/docs/messages/next-config-error
[build:next]
[build:next] > Build error occurred
[build:next] Error [ERR_REQUIRE_ESM]: require() of ES Module xxx/i18n.js from xxx/node_modules/next-translate-plugin/lib/cjs/index.js not supported.
[build:next] i18n.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
[build:next] Instead rename i18n.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in xxx/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).
[build:next]
[build:next] at nextTranslate (xxx/node_modules/next-translate-plugin/lib/cjs/index.js:26:14)
[build:next] at file:///xxx/next.config.mjs:147:10 {
[build:next] code: 'ERR_REQUIRE_ESM'
[build:next] }
This navigates me (at least to my knowledge) into a dead-lock (as the cjs next-translate wants a cjs i18n.js
and I can't rename it to .cjs
, because next-translate will complain about it missing).
Perhaps the first question I should be able to answer is: Why is next-translate-plugin/lib/cjs
running, not next-translate-plugin/lib/esm
?
Is there any known workaround to get it working with type: "module"
?
Also have with problem
Any ideas how to fix this?
I have the exact same issue. I wanted to use the i18n.js
version so I could change the path for importing locales, but for this issue to not block my work, I have temporarily converted to i18n.json
.
For me the issue happens because my projects package.json has defined type: "module"
, and yet the next-translate-plugin version used is the cjs.
Same for me
Although I can't use i18n.json because I have a custom loadLocaleFrom
I've been struggling with this the past day.
I tried to force the use of esm but did not work I tried to pass the i18n config directly to nextTranslatePlugin and it does not work either
Would be great to get seom help on this
Thanks
This is still an issue at v2.6.2