question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[vite] warning: Top-level "this" will be replaced with undefined since this file is an ECMAScript module

See original GitHub issue

Describe the bug

Running a React app with Vite has started producing the above error since version change of esmbuild from v0.14.44 to v0.14.45. This error occurs wherever the React app is using an arrow function. My reproduction below has created a fresh Vite app and only changed the App component from a function to a function expression using an arrow function and this error starts appearing.

[vite] hmr update /src/App.tsx
[vite] warning: Top-level "this" will be replaced with undefined since this file is an ECMAScript module
23 |          lineNumber: 11,
24 |          columnNumber: 9
25 |        }, this), /*#__PURE__*/_jsxDEV("p", {
   |           ^
26 |          children: "Hello Vite + React!"
27 |        }, void 0, false, {

  Plugin: vite:esbuild
  File: /Users/naresh/projects/vite-esmbuild-issue/src/App.tsx

The issue has been discussed in the esmbuild repository here and closed: https://github.com/evanw/esbuild/issues/2328. They need to know exactly what flags vite is using to invoke esmbuild.

Reproduction

https://github.com/nareshbhatia/vite-esmbuild-issue

System Info

System:
    OS: macOS 12.3.1
    CPU: (4) x64 Intel(R) Core(TM) i5-7600K CPU @ 3.80GHz
    Memory: 103.36 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.15.0 - ~/.nvm/versions/node/v16.15.0/bin/node
    Yarn: 1.22.15 - ~/.yarn/bin/yarn
    npm: 8.5.5 - ~/.nvm/versions/node/v16.15.0/bin/npm
    Watchman: 2022.03.21.00 - /usr/local/bin/watchman
  Browsers:
    Brave Browser: 96.1.32.115
    Chrome: 102.0.5005.115
    Safari: 15.4
  npmPackages:
    @vitejs/plugin-react: ^1.3.0 => 1.3.2
    vite: ^2.9.9 => 2.9.12

Used Package Manager

npm

Logs

No response

Validations

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:9
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

43reactions
sapphi-redcommented, Jun 17, 2022

This is happening because plugin-react uses @babel/plugin-transform-react-jsx to transform JSX.

For a workaround, you could set esbuild.logOverride to omit the warning.

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  esbuild: {
    logOverride: { 'this-is-undefined-in-esm': 'silent' }
  }
})
0reactions
YasirHasn9commented, Jun 21, 2022

My coworker comes up with this work around and it works. He adds this workaround and put them along side the rest of the configuration.

export default defineConfig({
  plugins: [
    process.env.MODE !== 'production'
      ? react({
          jsxRuntime: 'classic',
        })
      : react(),
    WindiCSS(),
  ],

  ... other properties
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

this is changed to undefined with webpack + typescript + ...
json { "compilerOptions": { "module": "esnext" // other configuration ... } } I'm trying to pass this as a parameter in one of...
Read more >
Troubleshooting - Vite
Vite cannot handle and does not support code that only runs on non-strict mode (sloppy mode). This is because Vite uses ESM and...
Read more >
Output - webpack
The top-level output key contains a set of options instructing webpack on how and where it should output your bundles, assets, and anything...
Read more >
vite warning top-level this will be replaced with ... - 掘金
vite warning top-level this will be replaced with undefined since this file is an ecmascript module技术、学习、经验文章掘金开发者社区搜索结果。
Read more >
Options - Babel.js
npx babel --root-mode upward file.js # equivalent of passing the rootMode config ... would allow plugins and presets to decide that, since ES...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found