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.

Some browsers run errors: 'globalThis' is not defined

See original GitHub issue

Describe the bug

最新版本 在浙政钉内置浏览器运行报错 ‘globalThis’ is not defined 使用@vitejs/plugin-legacy,依旧报同样的错误

    legacy({
      targets: ['chrome 51'],
    }),

外部版本比较低的浏览器可以显示,但有报错

Uncaught SyntaxError: Unexpected token import
(index):11 vite: loading legacy build because dynamic import is unsupported, syntax error above should be ignored

Reproduction

Since 2.6.0-beta.3 https://github.com/rashagu/viteTest3

System Info

System:
    OS: Windows 10 10.0.19043
    CPU: (4) x64 Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz
    Memory: 4.01 GB / 15.91 GB
  Binaries:
    Node: 16.13.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.15 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 8.1.0 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.19041.1266.0), Chromium (96.0.1054.41)
    Internet Explorer: 11.0.19041.1202

Used Package Manager

yarn

Logs

# 控制台 console
'globalThis' is not defined

# 控制台 console
Uncaught SyntaxError: Unexpected token import
(index):11 vite: loading legacy build because dynamic import is unsupported, syntax error above should be ignored

Validations

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
sodateacommented, Dec 4, 2021

无法根据你提供的代码复现问题。 如果以下方案无法解决问题,请提供更详细的环境信息(系统、浙政钉版本、浏览器 User-Agent 等)和正确的复现代码。


以下是根据目前问题描述进行的推测:

  1. 使用 plugin-legacy 后在外部低版本浏览器可以显示,证明 legacy 打包正常(注意报错第二行,Uncaught SyntaxError 是可以忽略的,这是 Vite 在探测 import() 语法支持时必要的报错)
  2. 据此推测,浙政钉加载运行了 <script type="module"> 的代码。
  3. 查询 https://caniuse.com/ 可知,globalThis 在 Chrome >= 71 支持,dynamic import 在 Chrome >= 63 支持。Vite 打包出来的 <script type="module"> 代码是针对所有支持 dynamic import 的浏览器的,即 Chrome >= 63。
  4. 浙政钉浏览器内核版本应该在 Chrome 63-70 之间;
  5. Vite 打包出来的 module 代码语法最低支持 Chrome 63,不会出错;但 globalThis 是全局变量而非新语法,语法转译不会涵盖,需要针对 modern build 另外引入 polyfill。
  6. 最简单的办法是用 legacy 插件的 modernPolyfills 选项:https://github.com/vitejs/vite/tree/main/packages/plugin-legacy#modernpolyfills ,即:
legacy({
  targets: ['Chrome 63'],
  modernPolyfills: true
})
0reactions
sodateacommented, Dec 4, 2021

1、这个不含如何业务代码的纯净例子在chrome版本 63.0.3223.0(开发者内部版本) (32 位) 浏览器中报新的错, 不含 legacy 插件

喔,我之前把这个搞错了「Vite 打包出来的 module 代码语法最低支持 Chrome 63」应该是「最低可以支持 Chrome 63」,默认还有 >= 0.5%, latest 2 versions 的条件所以不含 Chrome 63。(https://cn.vitejs.dev/guide/build.html#browser-compatibility)

试试再加上 build: { target: 'chrome63' } 的配置?再配合 legacy 后应该就不会再有问题了。

Read more comments on GitHub >

github_iconTop Results From Across the Web

ReferenceError: globalThis is not defined in old browsers #313
Run app in firefox. ErrorMessage: ReferenceError: globalThis is not defined. What can i do? I have several users in production and they get ......
Read more >
ReferenceError: globalThis is not defined - npm - Stack Overflow
Consider updating your version of node.js to >=12 . globalThis was added in node.js v12.0.0 according to this compatibility table.
Read more >
globalThis is not defined although node version is v14.4.0
If you experience errors saying that globalThis is not defined, be sure to run a recent version of Node.js (at least 12.0). node...
Read more >
JS broken on older web browsers due to undefined 'globalThis'
@DonaldDuck while the browser is unsupported this was introduced by the latest javascript changes. As with similar bugs these have been acted in ......
Read more >
ReferenceError: globalThis is not defined
Hey guys,. I am working on the ionic project and I am facing one issue. my app is working fine if I run...
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