"document" is not defined for xxxx.wasm?url
See original GitHub issueDescribe 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
- 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 vuejs/core 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 a year ago
- Reactions:2
- Comments:15 (6 by maintainers)
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:
Adding
self.document = {querySelectorAll(){return []}}
to the web worker silences the error.Are there any updates for this? The
self.document = {querySelectorAll(){return []}}
is put after the usage ofdocument
by the bundler so it still breaks for me. Inlining is also not an option