Skip to content
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

SyntaxError: Identifier 'global' has already been declared while importing a json file with "global" key in SSR environment #17698

Closed
7 tasks done
michaeljohnn opened this issue Jul 16, 2024 · 2 comments · Fixed by #17809
Labels
feat: ssr p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@michaeljohnn
Copy link

michaeljohnn commented Jul 16, 2024

Describe the bug

Error happened in a ssr vite react project while importing a json file as below:

index.tsx

import './test.json';

test.json

{
  "global": {
    "name": "全局"
  }
}

The error message is :

rror when evaluating SSR module /src/test.json:
|- SyntaxError: Identifier 'global' has already been declared
    at new AsyncFunction (<anonymous>)

Reproduction

https://github.com/korbinzhao/template-ssr-react-streaming-ts/blob/bug/ssr_json_global/src/App.tsx#L4

Steps to reproduce

No response

System Info

System:
    OS: macOS 13.5
    CPU: (8) arm64 Apple M1
    Memory: 173.59 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.11.1 - ~/.nvm/versions/node/v20.11.1/bin/node
    Yarn: 1.22.19 - ~/.yarn/bin/yarn
    npm: 10.2.4 - ~/.nvm/versions/node/v20.11.1/bin/npm
    pnpm: 8.14.1 - /opt/homebrew/bin/pnpm
  Browsers:
    Brave Browser: 102.1.39.111
    Chrome: 126.0.6478.127
    Edge: 114.0.1823.37
    Safari: 16.6

Used Package Manager

npm

Logs

No response

Validations

@bluwy
Copy link
Member

bluwy commented Jul 16, 2024

The global is being shadowed from

Internally Vite would generate code like this:

function (global, ...) {
    const global = "asd";
    Object.defineProperty(__vite_ssr_exports__, "global", { enumerable: true, configurable: true, get(){ return global }});
    __vite_ssr_exports__.default = {
    	global: global
    };
}

Which causes the issue because global can't be re-declared. Perhaps Vite needs to rewrite the internal global declaration to avoid the error.

@bluwy bluwy added p3-minor-bug An edge case that only affects very specific usage (priority) feat: ssr and removed pending triage labels Jul 16, 2024
@DemocracyAndScience
Copy link

I have encountered the same problem, checking if there is' global 'in your i18n. It has been tormenting me all afternoon, and it is actually this problem

@github-actions github-actions bot locked and limited conversation to collaborators Aug 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feat: ssr p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants