next.js: Turbopack: Bun APIs don't work with next
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
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 9
- Comments: 26 (2 by maintainers)
Commits related to this issue
- Mark bun builtin modules as external Fixes https://github.com/vercel/next.js/issues/75220 — committed to alii/next.js-two by Jarred-Sumner 2 years ago
Workaround for Using Turbopack with Bun APIs
Replace Direct Module Imports
Instead of importing modules from Bun like this:
Switch to using the namespaced version:
Patching Third-Party Libraries
If a third-party library still imports Bun modules directly, you can use Bun’s new
bun patchfeature. This feature allows you to persist changes in thenode_modulesdirectory across installations, and these modifications are versioned.Example: Fixing
drizzle-orm/bun-sqlWhen using
drizzle-orm/bun-sqlwith Next.js and Turbopack, you might see an error like this:To resolve the issue:
./node_modules/drizzle-orm/bun-sql/driver.js.SQLfrom'bun'.new SQL()withnew Bun.SQL()in the code.After making these changes, run the following command:
This command will:
patchesdirectory.patchedDependenciessection of yourpackage.json.Now, every time you run
bun i, your fixes will be automatically applied.@heymattiarighetti
A
Bun.versionwill fail, because you cased the module wrong - it’s supposed to be “bun”, which works for me: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.
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?Thankyou. This very helpful.
Why does this solve the issue?
A direct import need to be resolved by the bundler (turbopack) which currently doesn’t handle Bun specific apis well. But why do the name-spaced imports work ? - It’s because they’re aren’t resolved by the bundler. All of the
Bun.*functions are injected on runtime and omit the turbopack.Why doesn’t it work for
Bun. TransactionSQL?TransactionSQLdoes indeed extend the interface ofSQLbut this is not where the problem lies. It’s becauseBun.SQLis directly exposed on the global objectBunwhileBun.TransactionSQLis not.I hope this explains it.
Next will need to import if they spot this
Yes I think turbo pack and web pack need to also build for bun
@luudv do have example