js-lingui: Cannot process file /.tsx: Duplicate declaration "Trans"
Describe the bug
When running lingui extract I get the error Duplicate declaration "Trans" (“t” seems to be OK).
To Reproduce
Cannot process file /<path>.tsx: Duplicate declaration "Trans"
15 | // i18n
16 | import { useI18N, Locale } from 'i18n';
> 17 | import { t, Trans } from '@lingui/macro';
Expected behavior
No error on lingui extract
Additional context
- Tried v3.4.0 and still had issues
- CLI version v3.2.1 repros the issue
- CLI version v3.1.0 does not repro the issue
- @babel/core@7.12.10
- Using Lingui in a NextJS project
- Looked at the diff (https://github.com/lingui/js-lingui/compare/v3.1.0...v3.2.1) and there are two changes that jump out, but I don’t understand the code enough to troubleshoot part this point. (https://github.com/lingui/js-lingui/pull/846 and https://github.com/lingui/js-lingui/pull/854) (cc @semoal and @Bertg in case they have any ideas)
- .babelrc
{
"presets": ["next/babel"],
"plugins": ["macros"],
"env": {
"test": {
"plugins": ["dynamic-import-node"]
}
}
}
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 13
- Comments: 37 (13 by maintainers)
Commits related to this issue
- fix: NODE_ENV=production crashes lingui extract #952 — committed to lingui/js-lingui by deleted user 3 years ago
Thanks guys, changing
"extract": "lingui extract"to"extract": "NODE_ENV=development lingui extract"fixed it on my fresh install with NextJS. If I understood correctly what is said here I can keep it like that and not worry about it because it doesn’t need to haveNODE_ENV=productionwhen building for prod?The error is still there.
Duplicatedeclaration “Trans”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.
Looks like its still an issue. I reopen this and assign for myself.
This is still a thing
I fixed the issue on my nextjs project on windows 10, windows 11!!! Points are: 1 & 2 1. First of all you must install “npm i cross-env” on windows system (windows 10, 11). 2. You have to use “NODE_ENV=development” command Notice: cross-env NODE_ENV=development! cross-env NODE_ENV=development!! cross-env NODE_ENV=development!!! use below command on “package.json”
Hopeful help!
In my case the solution is to pass
babelrc: falsein the options here:https://github.com/lingui/js-lingui/blob/main/packages/cli/src/api/extractors/typescript.ts#L50-L55
Here is babelrc from my project:
The conflicting part is inside
babel-plugin-transform-react-remove-prop-typeswhich is enabled bynext/babelin isProduction mode (detected fromprocess.env:https://github.com/vercel/next.js/blob/canary/packages/next/build/babel/preset.ts
If
babel-plugin-transform-react-remove-prop-typesis configured with{ removeImport: true }(removes prop-types imports from code base), it will break the extract process. The error happens because of this line:https://github.com/oliviertassinari/babel-plugin-transform-react-remove-prop-types/blob/d3a618a9c72be08485f5e20659c917b300cf7b74/src/index.js#L364
I don’t know what
scope.crawldoes under the hood.Looks like my issue is not due to the bug inside lingui 😅. I’ll continue to use
NODE_ENV=developmentPossible solution here? In the meantime the band aid solution still seems to use cross-env and set
NODE_ENV=development.Not sure what exactly the fix for the library is but hopefully this can point someone in the right direction to fix this. I’m guessing this is impacting pretty much everyone trying to use this library right now.
I’m too using Next.js and I’ve been using the
NODE_ENV=developmentfix for quite a while, but today for some reason or another I started getting the same errors:I was using lingui 3.12.1 but I updated to the current latest 3.14.0.
I noticed that without using
NODE_ENV=developmentall files gave errors, but with using it about only 9 files gave errors. For these ones I replaced<Trans>macro withi18nfromuseLingui()and thetmacro instead.These fixed my errors one by one.
I think this should probably be re-opened. This is still an issue today with the latest version of lingui, as of writing that’s 3.13.2
After upgrading from v3.2.3 to v3.6.0 I encounter the same error. Forcing the NODE_ENV to development fix the issue.
Will try to take a deeper look if we can resolve this somehow internally in the Lingui CLI
Sounds good. For some more info…
I used
yarn reprowith the belowpackage.jsondefinition and it works.a.jsjust containsconsole.log(process.env.NODE_ENV)which prints outundefined.This
lingui:extract": "NODE_ENV=development lingui extractshould really get documented as all the people who use next.js will have to set this for the extraction to work properly.I think i got a fix for this “issue”, babel transform api allows to overwrite NODE_ENV variable internally, so probably won’t be required to use nODE_ENV=development before lingui extract command.
@ericvera next tuesday we’ll release a new version with this change introduced if you can tell us if this fixes the issue, would be amazing =)
Exactly, basically node_env=production applies changes to code that lingui can’t interpret