facebook / metro

🚇 The JavaScript bundler for React Native
https://metrobundler.dev
MIT License
5.24k stars 626 forks source link

Uncaught Error: Cannot access 'Object' before initialization #1331

Closed itsyoboieltr closed 2 months ago

itsyoboieltr commented 3 months ago

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

If there is a variable named Object

const Object = '';

present in a react native app, it throws the error:

Uncaught Error
Cannot access 'Object' before initialization

Quoting from the original issue raised in: https://github.com/sinclairzx81/typebox/issues/968

It is possible that the creation of a const Object = ... may be confusing the tooling where it may be interpreting the Object variable with globalThis.Object (which may or may not be polyfilled for the react native environment on build, I don't know). You should be able to repo the issue without by creating a Object variable (uppercase) and seeing if that triggers the error. I have seen similar behaviors before, but mostly they are related to something misbehaving in a tool or plugin somewhere.

If the current behavior is a bug, please provide the steps to reproduce and a minimal repository on GitHub that we can yarn install and yarn test.

  1. Clone the repository of the minimal reproduction
    git clone https://github.com/itsyoboieltr/bender-stack.git --branch object-error-reprod
  2. Change directory to the repository
    cd bender-stack
  3. Install dependencies
    bun i
  4. Run the development server
    bun dev
  5. Open http://localhost:3000 in your browser to see the error message
  6. (Optional) Comment the line const Object = ''; to make the example not throw the error message

What is the expected behavior?

It should be possible to have a variable named Object, as this is a valid javascript variable name. Libraries are breaking using Metro (for example, TypeBox) due to this.

Please provide your exact Metro configuration and mention your Metro, node, yarn/npm version and operating system.

Metro: default expo metro config. Node: v20.12.0 Bun: 1.1.26 Operating system: macOS, 14.5, 23F79

itsyoboieltr commented 2 months ago

Workaround suggested in https://github.com/expo/expo/issues/31167, it is actually a babel bug, not metro.

alexjalonso7777 commented 4 weeks ago

[](url)