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

{
  "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

Most upvoted comments

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 have NODE_ENV=production when building for prod?

The error is still there. Duplicate declaration “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”

    "lang:extract": "cross-env NODE_ENV=development lingui extract --clean",
  1. run “> yarn lang:extract” on console. each “messages.po” file created!
  2. installed package version as below “@lingui/react”: “^3.13.0”, “@lingui/cli”: “^3.13.0”, “@lingui/loader”: “^3.13.0”, “@lingui/macro”: “^3.13.0”,

Hopeful help!

In my case the solution is to pass babelrc: false in 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:

{
  "presets": [
    [
      "next/babel",
      {
        "preset-react": {
          "runtime": "automatic",
          "importSource": "@emotion/react"
        }
      }
    ]
  ],
  "plugins": ["@emotion/babel-plugin", "macros"]
}

The conflicting part is inside babel-plugin-transform-react-remove-prop-types which is enabled by next/babel in isProduction mode (detected from process.env:

https://github.com/vercel/next.js/blob/canary/packages/next/build/babel/preset.ts

If babel-plugin-transform-react-remove-prop-types is 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.crawl does under the hood.

Looks like my issue is not due to the bug inside lingui 😅. I’ll continue to use NODE_ENV=development

Possible 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=development fix for quite a while, but today for some reason or another I started getting the same errors:

Cannot process file /src/components/hooks/useNotifications.js: Duplicate declaration "Trans"
  1 | import React, { useEffect, useState, useContext, useRef } from 'react'
> 2 | import { Trans } from '@lingui/macro'
    |          ^^^^^

I was using lingui 3.12.1 but I updated to the current latest 3.14.0.

I noticed that without using NODE_ENV=development all files gave errors, but with using it about only 9 files gave errors. For these ones I replaced <Trans> macro with i18n from useLingui() and the t macro instead.

i18n._(t`...`)

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 repro with the below package.json definition and it works. a.js just contains console.log(process.env.NODE_ENV) which prints out undefined.

"scripts": {
   "repro" : "cross-env NODE_ENV=development lingui extract && node a.js"
   ...
}

This lingui:extract": "NODE_ENV=development lingui extract should 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