bun icon indicating copy to clipboard operation
bun copied to clipboard

Can't find variable: window

Open Wedyarit opened this issue 2 years ago • 10 comments

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.

Wedyarit avatar Sep 10 '23 17:09 Wedyarit

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.

jamesdh avatar Sep 16 '23 15:09 jamesdh

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 avatar Oct 20 '23 19:10 pablo-mayrgundter

@pablo-mayrgundter do you use testing-library? Even if I specify global.document this way, tesing-library still claims document is undefined

uladzimirdev avatar Oct 20 '23 19:10 uladzimirdev

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

pablo-mayrgundter avatar Oct 20 '23 22:10 pablo-mayrgundter

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.

pdigre avatar Oct 22 '23 20:10 pdigre

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

AmirMuha avatar Nov 18 '23 09:11 AmirMuha

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.

klausbreyer avatar Mar 08 '24 10:03 klausbreyer

Same problem here with the same error I hope the bun team fix this soon.

aymammeri avatar Mar 11 '24 09:03 aymammeri

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.

jamesdh avatar Mar 12 '24 16:03 jamesdh

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",
   }

SadmanYasar avatar Apr 07 '24 01:04 SadmanYasar