oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.21k stars 2.77k forks source link

ReferenceError: Bun is not defined #5263

Open bobbymannino opened 1 year ago

bobbymannino commented 1 year ago

What version of Bun is running?

1.0.1+31aec4ebe325982fc0ef27498984b0ad9969162b

What platform is your computer?

Darwin 23.0.0 arm64 arm

What steps can reproduce the bug?

Create a SvelteKit project and then install bun-types. Try and add Bun.version into the code anywhere - client or server - throws an error.

What is the expected behavior?

It causes a 500 error

What do you see instead?

No response

Additional information

Am i doing something wrong?

g-ioannou commented 1 year ago

Got the same issue with sveltekit on Darwin 22.3.0 arm64 arm.

Used Bun.password which ran initally, but got ReferenceError: Bun is not defined after a while.

Using Bun.password outside of of the sveltekit project works as expected.

Jarred-Sumner commented 1 year ago

It’s running in Node. Make sure you pass -b or —bun

bobbymannino commented 1 year ago

Tried that as well. Get this error

failed to load config from /Users/bobbymannino/Library/Mobile Documents/com~apple~CloudDocs/Sites/Demos/view-transitions-api-demo/vite.config.ts
error when starting dev server:
undefined
error: script "dev" exited with code 1 (SIGHUP)

And i have installed the bun sveltekit adapter too

fractal-solutions commented 1 year ago

Getting the same issue using Bun.sleepSync(100) inside the loader.load() function of a GLTFLoader in Three.js.

IMG_20230913_202221_114.jpg

ThatXliner commented 1 year ago

Tried that as well. Get this error

failed to load config from /Users/bobbymannino/Library/Mobile Documents/com~apple~CloudDocs/Sites/Demos/view-transitions-api-demo/vite.config.ts
error when starting dev server:
undefined
error: script "dev" exited with code 1 (SIGHUP)

And i have installed the bun sveltekit adapter too

Had this error to when i was trying to run SvelteKit with bun --bun. (Bun 1.0.0+822a00c4d508b54f650933a73ca5f4a3af9a7983, Darwin 22.5.0 x86_64 i386). This issue is reported here as well (I haven’t found the oldest issue that reports this problem though)

emircanerkul commented 1 year ago

+1, I also observed the same problem while using sveltekit +page.svelte

wpmcdonnell commented 11 months ago

Same with Vite + React + SWC

jokroese commented 11 months ago

I'm experiencing this issue with SvelteKit too. Is there anything I can do to help investigate this?

MosheRivkin commented 11 months ago

I'm experiencing this issue with SvelteKit too. Is there anything I can do to help investigate this?

Vite's CLI use node by default. try to add bunx --bun to the scripts in package.json

"scripts": {
    "dev": "bunx --bun vite",
    "build": "bunx --bun vite build",
    "preview": "bunx --bun vite preview",
}

sorce: Build a frontend using Vite and Bun

fran-cadenas-fu commented 11 months ago

I'm getting the same error on NextJS rout handlers. It suddenly stopped working.

notramo commented 11 months ago

Just to be clear, bun-types is only TS definitions, does not add any JS functions/objects to the global scope, only TS types. This means that the global Bun object is not available in the browser (the above screenshot implies it's accessed from code that runs in the browser).

Bun-specific code can be used only +page.server.ts and +server.ts, but requires a typeof Bun !== 'undefined' check when used in +page.svelte or +page.ts, so it's only accessed during SSR.

kapitche commented 11 months ago

I'm getting the same error on NextJS route handlers. It suddenly stopped working.

Same here, can't hash my data using Bun.password because then I get

 ⨯ ReferenceError: Bun is not defined
    at POST (webpack-internal:///(rsc)/./src/app/api/register/route.ts:72:32)
    at async /Users/USERNAME/Documents/GitHub/PROJECT/node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:62609

Does anyone have any idea?

fran-cadenas-fu commented 11 months ago

I'm getting the same error on NextJS route handlers. It suddenly stopped working.

Same here, can't hash my data using Bun.password because then I get

 ⨯ ReferenceError: Bun is not defined
    at POST (webpack-internal:///(rsc)/./src/app/api/register/route.ts:72:32)
    at async /Users/USERNAME/Documents/GitHub/PROJECT/node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:62609

Does anyone have any idea?

In my case it was a problem of the all routes handler.

kapitche commented 11 months ago

I'm getting the same error on NextJS route handlers. It suddenly stopped working.

Same here, can't hash my data using Bun.password because then I get

 ⨯ ReferenceError: Bun is not defined
    at POST (webpack-internal:///(rsc)/./src/app/api/register/route.ts:72:32)
    at async /Users/USERNAME/Documents/GitHub/PROJECT/node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:62609

Does anyone have any idea?

In my case it was a problem of the all routes handler.

Did you find a fix yet?

ko-devHong commented 8 months ago

I'm experiencing this issue with SvelteKit too. Is there anything I can do to help investigate this?

Vite's CLI use node by default. try to add bunx --bun to the scripts in package.json

"scripts": {
  "dev": "bunx --bun vite",
  "build": "bunx --bun vite build",
  "preview": "bunx --bun vite preview",
}

sorce: Build a frontend using Vite and Bun

I'm using my custom cli library. it works if I use your command. Thanks

prisius commented 1 week ago

i have this in my package.json

"scripts": {
    "dev": "bunx --bun vite",
    "build": "bunx --bun vite build",
    "preview": "bunx --bun vite preview",
}

still can't get bun working. I got bun is not defined.

I am using vite vanilla javascript. I have the latest version of Bun. How to get sure if vite is not using node ?

bobbymannino commented 1 week ago

i think they changed it to now have a space after bun in bunx bun x --bun vite

I do not like the fact i have to explicitly type --bun. if i put into the terminal bun run dev and the script is vite i expect the dev server to run on Bun (not directly node), having to use x and --bun is too much of an abstraction compared to node, i thought bun was a drop in replacement?

MosheRivkin commented 1 week ago

I am using vite vanilla javascript. I have the latest version of bun . How to get sure if vite is not using node ?

since you're using the vite vanilla JS template, which doesn't include SSR, I assume you're trying to use the bun API (Bun.*) from the frontend. bun's API (as well as node's API) isn't accessible in the browser (as @notramo mentioned here), hence the error, but it dose serve the html using bun, as long as you're using the --bun flag.

if you want to see that vite is using bun and not node to serve the files, you can just inspect the process. For example, I'm on Windows, so I'll use the widows Task Manager:

when I run bun vite, I see node used in the process: Image

but when I use bun --bun vite (or bunx --bun vite), bun is used to serve the files: Image

MosheRivkin commented 1 week ago

i think they changed it to now have a space after bun in bunx bun x --bun vite

i think that the change was that you can type bun instead of bunx to run vite, so you can type bun --bun vite (or maybe it was possible all the time🤔 anyway back then the example in the docs was with bunx)

prisius commented 1 week ago

i think they changed it to now have a space after bun in bunx bun x --bun vite

i think that the change was that you can type bun instead of bunx to run vite, so you can type bun --bun vite (or maybe it was possible all the time🤔 anyway back then the example in the docs was with bunx)

bun --bun vite

won't work.

I tried on Windows and Ubuntu. Same problem.

What am i doing wrong ?

prisius commented 1 week ago

i think they changed it to now have a space after bun in bunx bun x --bun vite

i think that the change was that you can type bun instead of bunx to run vite, so you can type bun --bun vite (or maybe it was possible all the time🤔 anyway back then the example in the docs was with bunx)

bun --bun vite

won't work.

I tried on Windows and Ubuntu. Same problem.

What am i doing wrong ?

Removing nodejs didn't work either.

prisius commented 1 week ago

bun init works !

I think it has something to do with vite.

Somehow vite doesn't recognize Bun with vanilla js and typescript. I didn't tested with React.