"document" is not defined for xxxx.wasm?url

See original GitHub issue

Describe the bug

Hi there,

the following error is fired when I use latest version 3.0.9 to compile the following code in production mode:

import xxxx from “…/…/xxx.wasm?url”;

The error is as follows. “document” is not defined

So I switch back to old version 2.9.x and the error disappear

After some investigation, I found it is caused by the changeset:

  • fix: avoid using import.meta.url for relative assets if output is not ESM (fixes #9297) (#9381)

The following code doesn’t check document variable:

const getRelativeUrlFromDocument = (relativePath: string, umd = false) => getResolveUrl( '${relativePath}', ${ umd ? typeof document === ‘undefined’ ? location.href : : '' }document.currentScript && document.currentScript.src || document.baseURI )

So I suggest to add a simple “typeof document !== ‘undefined’” in the last sentence.

Reproduction

https://stackblitz.com/edit/vitejs-vite-85cthg?file=vite.config.js

run “npm run build” then “vite preview”

System Info

"document" is not defined

Used Package Manager

yarn

Logs

No response

Validations

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:2
  • Comments:15 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
mustafa0xcommented, Nov 9, 2022

I have a similar error, using vite, svelte, web worker (via comlink).

When I edit the svelte component (so it seems to be caused by HMR) I see the error below:

client.ts:288 Uncaught (in promise) ReferenceError: document is not defined
    at hasErrorOverlay (client.ts:288:3)
    at handleMessage (client.ts:150:28)
    at WebSocket.<anonymous> (client.ts:91:5)

Adding self.document = {querySelectorAll(){return []}} to the web worker silences the error.

1reaction
seanayecommented, Nov 17, 2022

Are there any updates for this? The self.document = {querySelectorAll(){return []}} is put after the usage of document by the bundler so it still breaks for me. Inlining is also not an option

Read more comments on GitHub >

github_iconTop Results From Across the Web

Uncaught ReferenceError: $ is not defined at xxxx.php:4
I am trying out this http://jsfiddle.net/fy63opLq/ The script works on this URL, but when I do it locally, it gave me this error...
Read more >
Local set - WebAssembly - MDN Web Docs - Mozilla
The local.set instruction sets the values of a local variable.
Read more >
js if 优化- OSCHINA - 中文开源技术交流社区
... 查找(因为涉及作用域上的查找) function updateUI() { var imgs = document. ... //ouput: undefined 回到顶部==和===的区别避免在if和while语句的条件部分进行 ...
Read more >
Uncaught referenceerror is not defined | Edureka Community
$(document).ready(function() $('#tabs > ul').tabs({ fx: { opacity: 'toggle' } }); $('#featuredvid > ...
Read more >
vite - bytemeta
vite2.9.15 + ant-design-vue1.7.8 + vue2 build after ERROR:Uncaught ReferenceError: Antd is not defined ... "document" is not defined for xxxx.wasm?url.
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