bun
bun copied to clipboard
Can't find variable: window
What version of Bun is running?
1.0.0
What platform is your computer?
Darwin 22.6.0 arm64 arm
What steps can reproduce the bug?
Error is being thrown when I try to run
bun dist/main.js
On my NestJS + AdminJS project.
What is the expected behavior?
I expect NestJS project to run.
What do you see instead?
ReferenceError: Can't find variable: window at module code (***/node_modules/adminjs/lib/backend/utils/view-helpers/view-helpers.js:3:2) at processTicksAndRejections (:1:1302)
Additional information
I guess the problem happens because Bun does not support AdminJS yet. So that this might be rather feature request, than bug report.
Decided to tinker around with Bun this Saturday morning, and stumbled onto this. Per their docs:
Some Web APIs aren't relevant in the context of a server-first runtime like Bun, such as the DOM API or History API.
So I would presume that it's fairly unlikely the Bun team would do anything to resolve this. It's simply not their focus.
These are easy enough to setup with jsdom, e.g.:
# Theme.test.jsx
import {JSDOM} from 'jsdom'
const dom = new JSDOM(`<!DOCTYPE html>`, {pretendToBeVisual: true})
global.window = dom.window
global.document = window.document
// commented out rest of test. Just above fails
But I do get an error currently when I run this:
> bun test src/theme/Theme.test.jsx
bun test v1.0.6 (969da088)
src/theme/Theme.test.jsx:
dyld[34585]: missing symbol called
Killed: 9
@pablo-mayrgundter do you use testing-library? Even if I specify global.document this way, tesing-library still claims document is undefined
Indeed, the dom and jest-dom module:
testing-library/dom: "^8.19.1",
testing-library/jest-dom: "^5.16.5",
note, testing-library is @testing-library, but GH was overwriting it as an email
On Fri, Oct 20, 2023 at 2:45 PM Uladzimir Havenchyk < @.***> wrote:
@pablo-mayrgundter https://github.com/pablo-mayrgundter do you use testing-library? Even if I specify global.document this way, tesing-library still claims document is undefined
— Reply to this email directly, view it on GitHub https://github.com/oven-sh/bun/issues/4837#issuecomment-1773308338, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAS5V34G63FO7B5XSJZCWDTYALIG3AVCNFSM6AAAAAA4SJ7TNSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONZTGMYDQMZTHA . You are receiving this because you were mentioned.Message ID: @.***>
-- interweb homepage http://sites.google.com/site/pablomayrgundter
I am having a similar issue with a web-components SPA app where "bun build" bundles and runs it, but using "bun run" or "bun --watch" fails ReferenceError: Can't find variable: document on "document", "window", "customElements", etc.
I'm facing the same problem with the exact same error message as the title of this issue, while trying bun run src/index.tsx
Stumbled also uppon this here, while I was evaluating if bun bundler is a replacement for parcel. But I guess not and we still need a traditional bundler for web apps.
Same problem here with the same error I hope the bun team fix this soon.
Let's try this again. Bun is a server-first runtime. Per Bun's own docs:
Some Web APIs aren't relevant in the context of a server-first runtime like Bun, such as the DOM API or History API.
Window is part of the DOM API. Hence, you can safely assume that not only is Bun not going to fix this, but also that for Bun's intended purpose, this isn't even a bug. It's literally by design.
This error has been fixed last week in AdminJS https://github.com/SoftwareBrothers/adminjs/pull/1623
I had to upgrade to the latest versions of AdminJS and Bun and updated CommonJS code for AdminJS(require and module.exports) to ESM(import and export).
Before:
const AdminJS = require('adminjs');
//rest of the config
module.exports = router;
After:
import AdminJS from 'adminjs';
//rest of the config
export default router
Here are the versions for reference that works for me: Bun on Windows: v1.1.1
package.json:
"dependencies": {
"@adminjs/express": "^6.1.0",
"@adminjs/import-export": "^3.0.0",
"@adminjs/mongoose": "^4.1.0",
"adminjs": "^7.8.1",
}