Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turbopack: Bun APIs don't work with next #75220

Open
brrock opened this issue Jan 23, 2025 · 18 comments
Open

Turbopack: Bun APIs don't work with next #75220

brrock opened this issue Jan 23, 2025 · 18 comments
Labels
linear: turbopack Confirmed issue that is tracked by the Turbopack team. Turbopack Related to Turbopack with Next.js.

Comments

@brrock
Copy link

brrock commented Jan 23, 2025

Link to the code that reproduces this issue

https://github.com/brrock/buntestapis/

To Reproduce

Start the app with bun run --bun dev --turbo pack and use the form the api route should fail to compile even though I'm using bun and have bun types installed

Current vs. Expected behavior

I expect this to just use the bun runtime and compile and work simply work but throws this error Error:

 ✓ Compiled /api/s3write in 377ms
 ⨯ ./app/api/s3write/route.ts:2:1
Module not found: Can't resolve 'bun'
  1 | // app/api/s3write/route.ts
> 2 | import { S3Client } from "bun";
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  3 | import { NextResponse } from 'next/server';
  4 |
  5 | // Initialize S3 client

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: kernel 6.12.9-arch1-1
  Available memory (MB): 15763
  Available CPU cores: 4
Binaries:
  Node: 23.6.0
  npm: 10.9.2
  Yarn: N/A
  pnpm: 9.15.4
Relevant Packages:
  next: 15.1.6 // Latest available version is detected (15.1.6).
  eslint-config-next: 15.1.6
  react: 19.0.0
  react-dom: 19.0.0
  typescript: 5.7.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Runtime

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

No response

@github-actions github-actions bot added the Runtime Related to Node.js or Edge Runtime with Next.js. label Jan 23, 2025
@luudv
Copy link

luudv commented Jan 23, 2025

It seems there are many bugs with the new version. I had to go back to v15.1.5

@brrock2
Copy link

brrock2 commented Jan 23, 2025

It seems there are many bugs with the new version. I had to go back to v15.1.5

it has always been a problem

@brrock
Copy link
Author

brrock commented Jan 23, 2025

It seems there are many bugs with the new version. I had to go back to v15.1.5

@luudv do have example

@samcx samcx removed the Runtime Related to Node.js or Edge Runtime with Next.js. label Jan 28, 2025
@LarsKumbier
Copy link

I have the same problem with the import { sql } from "bun". I've added @types/bun and am directly running bun --bun run node_modules/.bin/next dev --turbopack:

 ⨯ ./app/query/route.ts:2:1
Module not found: Can't resolve 'bun'
  1 | import pg from "pg";
> 2 | import { sql } from "bun";
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^

(ignore the unnecessary first code line)

Environment:

  • Bun: v1.2.1
  • NextJS v15.1.6

@LarsKumbier
Copy link

LarsKumbier commented Jan 30, 2025

Update: the problem seems to be turbopack. Omiting the --turbopack-option in the commandline, I can properly use bun-native APIs like sql in the dev server as well as in a production build.
@samcx Is this still the right place to work on this issue, since turbopack is so tightly coupled with NextJS? Or should I cross-post to turborepo github?

@brrock
Copy link
Author

brrock commented Jan 30, 2025

Yes I think turbo pack and web pack need to also build for bun

@brrock
Copy link
Author

brrock commented Jan 30, 2025

Update: the problem seems to be turbopack. Omiting the --turbopack-option in the commandline, I can properly use bun-native APIs like sql in the dev server as well as in a production build.
@samcx Is this still the right place to work on this issue, since turbopack is so tightly coupled with NextJS?

Thanks will try

@brrock
Copy link
Author

brrock commented Jan 30, 2025

It could be napi-rs (turbo pack builder)producing node code and doesn't know about bun

@brrock
Copy link
Author

brrock commented Jan 30, 2025

@samcx this turbopack error

@brrock brrock changed the title Bun APIs don't work with next Turbopack: Bun APIs don't work with next Jan 30, 2025
@samcx
Copy link
Member

samcx commented Jan 31, 2025

@LarsKumbier This is the right place for Turbopack issues!

@LarsKumbier
Copy link

@samcx Thank you - any more information I can provide for the replication of the issue or debugging?

@heymattiarighetti
Copy link

Can't get my head around it and make it work...

I was on NextJS 15.1.6 with turbopack on

  • Tried without turbopack: ❌
  • Downgraded to NextJS 15.1.5: ❌
  • Downgraded to 15.1.5 without turbopack: ❌
Module not found: Can't resolve 'bun'
> 1 | import { s3 } from "bun";

@LarsKumbier
Copy link

@heymattiarighetti it definetly works for me without turbopack.

  • did you install @types/bun?
  • did you make sure that you enforce nextjs to run with bun instead of nodejs, and without the default --turbopack option?
$ bun --bun run node_modules/.bin/next dev

Just tested it again (successfully) with Next.JS 15.1.6. If it still doesn't work for you, please start the dev server and post a ps -Af with the relevant processes.

@heymattiarighetti
Copy link

@LarsKumbier yes, I later realized it works with turbopack and the latest NextJS...

What doesn't work instead is the built s3 object support, even when running it from bun instead of nodejs

These are my processes:

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName                                
-------  ------    -----      -----     ------     --  -- -----------                                
    139       9    79080      41804       0,06  15176   5 bun                                        
     56       4      656       2964       0,00  13612   5 next                                       

but the error persists

Module not found: Can't resolve 'bun'
> 1 | import { s3 } from "bun";

I also tried it on a completely new NextJS project (with the correct @types/bun and everything)

What's weird is that even a simple console.log(Bun.version); returns a [ Server ] Error: Bun is not defined 🤷‍♂

I'm losing my mind on this!

@LarsKumbier
Copy link

@heymattiarighetti

A Bun.version will fail, because you cased the module wrong - it's supposed to be "bun", which works for me:

import { version } from "bun";
console.log(version);

But it absolutely does not work with --turbopack, even in the newest version, and even in the newest canary version (15.2.0-canary.41). My guess is that you did the same mistake I did and did not use the import - which will consequently be removed by turbopack, so it looks like its working, but in reality, it does not:

// with turbopack
$ bun --bun run node_modules/.bin/next dev --turbopack
   ▲ Next.js 15.2.0-canary.41 (Turbopack)
   - Local:        http://localhost:3000
   - Network:      http://192.168.178.53:3000
   - Environments: .env
   - Experiments (use with caution):
     · ppr

 ✓ Starting...
 ✓ Ready in 986ms
 ○ Compiling /seed ...
 ✓ Compiled /seed in 687ms
 ⨯ ./app/seed/route.ts:2:1
Module not found: Can't resolve 'bun'
  1 | import bcrypt from "bcrypt";
> 2 | import { sql, TransactionSQL } from "bun";
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

[...]

// without turbopack
$ bun --bun run node_modules/.bin/next dev
   ▲ Next.js 15.2.0-canary.41
   - Local:        http://localhost:3000
   - Network:      http://192.168.178.53:3000
   - Environments: .env
   - Experiments (use with caution):
     · ppr

 ✓ Starting...
 ✓ Ready in 1391ms
 ○ Compiling /seed ...
 ✓ Compiled /seed in 1392ms (406 modules)
 GET /seed 200 in 1609ms

AFAIK, you must not use turbopack for the time being.

@brrock
Copy link
Author

brrock commented Feb 5, 2025

Yep @samcx this 100% turbo pack anyone willing to work on this and possibly add deno support

@brrock
Copy link
Author

brrock commented Feb 5, 2025

@LarsKumbier yes, I later realized it works with turbopack and the latest NextJS...

What doesn't work instead is the built s3 object support, even when running it from bun instead of nodejs

These are my processes:

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName                                
-------  ------    -----      -----     ------     --  -- -----------                                
    139       9    79080      41804       0,06  15176   5 bun                                        
     56       4      656       2964       0,00  13612   5 next                                       

but the error persists

Module not found: Can't resolve 'bun'
> 1 | import { s3 } from "bun";

I also tried it on a completely new NextJS project (with the correct @types/bun and everything)

What's weird is that even a simple console.log(Bun.version); returns a [ Server ] Error: Bun is not defined 🤷‍♂

I'm losing my mind on this!

Next will need to import if they spot this

@heymattiarighetti
Copy link

@heymattiarighetti

But it absolutely does not work with --turbopack, even in the newest version, and even in the newest canary version (15.2.0-canary.41). My guess is that you did the same mistake I did and did not use the import - which will consequently be removed by turbopack, so it looks like its working, but in reality, it does not:

[...]

AFAIK, you must not use turbopack for the time being.

I'm not sure what changed between my earlier tests and this, but now it works! FINALLY!

This is how it ran:

bun --bun run node_modules/.bin/next dev

More specifically, I have the "dev": "next dev" script in my package, so I did bun --bun run dev

Also, set up the custom script on Vercel and it works fine

(waiting the --turbopack fix now 😁)

Thanks a lot, @LarsKumbier!!

@samcx samcx added Turbopack Related to Turbopack with Next.js. linear: turbopack Confirmed issue that is tracked by the Turbopack team. labels Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
linear: turbopack Confirmed issue that is tracked by the Turbopack team. Turbopack Related to Turbopack with Next.js.
Projects
None yet
Development

No branches or pull requests

6 participants