Some browsers run errors: 'globalThis' is not defined
See original GitHub issueDescribe 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
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it’s a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/vue-next instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
无法根据你提供的代码复现问题。 如果以下方案无法解决问题,请提供更详细的环境信息(系统、浙政钉版本、浏览器 User-Agent 等)和正确的复现代码。
以下是根据目前问题描述进行的推测:
<script type="module">
的代码。globalThis
在 Chrome >= 71 支持,dynamic import
在 Chrome >= 63 支持。Vite 打包出来的<script type="module">
代码是针对所有支持 dynamic import 的浏览器的,即 Chrome >= 63。globalThis
是全局变量而非新语法,语法转译不会涵盖,需要针对 modern build 另外引入 polyfill。modernPolyfills
选项:https://github.com/vitejs/vite/tree/main/packages/plugin-legacy#modernpolyfills ,即:喔,我之前把这个搞错了「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 后应该就不会再有问题了。