-
-
Notifications
You must be signed in to change notification settings - Fork 764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for ES module syntax in next-i18next.config.js
#1505
Comments
We can release this in the v9.0.0 major. I'd certainly appreciate help if you have time to look into it.
The If we standardise/force the |
Is there an update to this issue? The last time i tried to use "next-i18next.config.mjs" in combination with "next.config.mjs" i got some problems. |
No update. @LaCocoRoco Would you like to look into it and contribute? |
This is probably way out of my league. If i got some free time i will take a look into it and if i have a solution i will give a response regarding the problem. |
Supporting the cjs syntax would be a nice stopgap |
@jlarmstrongiv PRs welcome. |
@isaachinman error - ../next-i18next/dist/esm/config/createConfig.js:19:0
Module not found: Can't resolve 'fs'
17 |
18 | import { defaultConfig } from './defaultConfig';
> 19 | import fs from 'fs';
20 | import path from 'path';
21 | var deepMergeObjects = ['backend', 'detection'];
22 | export var createConfig = function createConfig(userConfig) {
Import trace for requested module:
../next-i18next/dist/esm/appWithTranslation.js
../next-i18next/dist/esm/index.js
./src/pages/_app.tsx
https://nextjs.org/docs/messages/module-not-found
|
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
I hate bots |
Hey everyone. Before opening a PR to support const DEFAULT_CONFIG_PATH = './next-i18next.config.js'
const DEFAULT_CONFIG_PATH_CJS = './next-i18next.config.cjs'
[...]
if (!userConfig) {
if (fs.existsSync(path.resolve(DEFAULT_CONFIG_PATH))) {
userConfig = await import(path.resolve(DEFAULT_CONFIG_PATH))
} else if (fs.existsSync(path.resolve(DEFAULT_CONFIG_PATH_CJS))) {
userConfig = await import(path.resolve(DEFAULT_CONFIG_PATH_CJS))
}
}
[...] I added it locally but I don't understand how the testing works? |
What about mjs? @AhmedBHameed |
Well I made an attempt at https://github.com/otterDeveloper/next-i18next/tree/esm-config-support-1 Importing cjs works but I didn't have any luck with mjs. It either cant find the mjs file: or because the package has support for CommonJS, in another test project, it throws an error about importing an ESM module using require Even Jest doesn't like importing mjs So I think this is a no go right now A mitigation would be to make it clear in the Readme that the config must be named exactly |
@otterDeveloper have you tried the |
Maybe I should open another issue for this but just to add to the conversation: For a use case example, as I tried to have only one source of truth for the list of supported locales, I used to do something like this in my // @ts-check
const fs = require('fs/promises');
module.exports = async (/* phase, { defaultConfig } */) => {
const locales = await fs.readdir('./public/locales');
const defaultLocale = 'default';
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
i18n: {
locales: [defaultLocale, ...locales],
defaultLocale,
localeDetection: false,
},
};
} |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
not stale |
this issue should be open |
Any workaround? |
This is still an issue that is not addressed. There are too many benefits to using mjs to not have a work around for this. |
Almost 2 years still nothing! yikes |
feel free to provide a PR if you like it |
不使用mjs也不使用cjs后缀就直接写成js即可 // eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require('path');
const languagesConfig = [
{
label: 'English',
value: 'EN',
},
{
label: '中文',
value: 'ZH_CN',
},
];
const i18n = {
defaultLocale: 'ZH_CN',
locales: languagesConfig.map((language) => language.value),
localePath: path.resolve('./public/locales'),
};
module.exports = {
i18n,
languagesConfig,
}; import withBundleAnalyzer from "@next/bundle-analyzer"
import withPlugins from "next-compose-plugins"
import { env } from "./env.mjs"
import { i18n } from "./next-i18next.config.js";
/**
* @type {import('next').NextConfig}
*/
const config = withPlugins([[withBundleAnalyzer({ enabled: env.ANALYZE })]], {
reactStrictMode: true,
experimental: { instrumentationHook: true },
rewrites() {
return [
{ source: "/healthz", destination: "/api/health" },
{ source: "/api/healthz", destination: "/api/health" },
{ source: "/health", destination: "/api/health" },
{ source: "/ping", destination: "/api/health" },
]
},
head: {
link: [
{
rel: 'icon',
href: '/favicon.ico',
},
],
},
i18n
})
export default config 我这样写可以使用 |
Why isn't this reopened? |
reopen |
Is your feature request related to a problem? Please describe.
Next.js 12 supports ES module syntax for the
next.config
file if the file extension is.mjs
. Creatingnext-i18next.config.mjs
doesn't work, however, sincenext-i18next
looks specifically for anext-i18next.config.js
file.Describe the solution you'd like
next-i18next
should support ES module syntax for thenext-i18next.config.js
file.Describe alternatives you've considered
I can achieve this by manually importing a
next-i18next.config.js
file and passing it toserverSideTranslations
, but this is burdensome to do for apps with lots of pages.Additional context
It was very surprising to me that
next-i18next
requires the existence of anext-i18next.config.js
file. It'd be cool if in addition to adding ES modules support,next-i18next
would grab its settings from thenext.config.js
file in the absence of anext-i18next.config.js
file.The text was updated successfully, but these errors were encountered: