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

Most upvoted comments

Workaround for Using Turbopack with Bun APIs

Replace Direct Module Imports

Instead of importing modules from Bun like this:

import { SQL } from 'bun';

const client = new SQL();

Switch to using the namespaced version:

const client = new Bun.SQL();

Patching Third-Party Libraries

If a third-party library still imports Bun modules directly, you can use Bun’s new bun patch feature. This feature allows you to persist changes in the node_modules directory across installations, and these modifications are versioned.

Example: Fixing drizzle-orm/bun-sql

When using drizzle-orm/bun-sql with Next.js and Turbopack, you might see an error like this:

https://nextjs.org/docs/messages/module-not-found

 ⨯ ./node_modules/drizzle-orm/bun-sql/driver.js:1:1
Module not found: Can't resolve 'bun'
> 1 | import { SQL } from "bun";
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^
  2 | import { entityKind } from "../entity.js";
  3 | import { DefaultLogger } from "../logger.js";
  4 | import { PgDatabase } from "../pg-core/db.js";

To resolve the issue:

  1. Open the file located at ./node_modules/drizzle-orm/bun-sql/driver.js.
  2. Remove the import statement for SQL from 'bun'.
  3. Replace all instances of new SQL() with new Bun.SQL() in the code.

After making these changes, run the following command:

bun patch --commit 'node_modules/drizzle-orm'

This command will:

  • Create a patch file in the patches directory.
  • Add an entry in the patchedDependencies section of your package.json.

Now, every time you run bun i, your fixes will be automatically applied.

@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.

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?

Workaround for Using Turbopack with Bun APIs

Replace Direct Module Imports

Instead of importing modules from Bun like this:

import { SQL } from ‘bun’;

const client = new SQL(); Switch to using the namespaced version:

const client = new Bun.SQL(); Patching Third-Party Libraries

If a third-party library still imports Bun modules directly, you can use Bun’s new bun patch feature. This feature allows you to persist changes in the node_modules directory across installations, and these modifications are versioned.

Example: Fixing drizzle-orm/bun-sql

When using drizzle-orm/bun-sql with Next.js and Turbopack, you might see an error like this:

https://nextjs.org/docs/messages/module-not-found

 ⨯ ./node_modules/drizzle-orm/bun-sql/driver.js:1:1
Module not found: Can't resolve 'bun'
> 1 | import { SQL } from "bun";
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^
  2 | import { entityKind } from "../entity.js";
  3 | import { DefaultLogger } from "../logger.js";
  4 | import { PgDatabase } from "../pg-core/db.js";

To resolve the issue:

  1. Open the file located at ./node_modules/drizzle-orm/bun-sql/driver.js.
  2. Remove the import statement for SQL from 'bun'.
  3. Replace all instances of new SQL() with new Bun.SQL() in the code.

After making these changes, run the following command:

bun patch --commit ‘node_modules/drizzle-orm’ This command will:

  • Create a patch file in the patches directory.
  • Add an entry in the patchedDependencies section of your package.json.

Now, every time you run bun i, your fixes will be automatically applied.

Thankyou. This very helpful.

@MichalBastrzyk That’s a nice workaround, thank you - could you shed some light on why this the namespaced version works, while the direct import does not?

One caveat: This does work nicely for Bun.SQL(), but does not for Bun.TransactionSQL() - despite this being interface being straight extended from SQL: https://github.com/oven-sh/bun/blob/aa2e109f5fd11cd34c1e70acd4997ce0e6e692e6/packages/bun-types/bun.d.ts#L2302-L2306

I don’t understand why - could you help here as well?

As for the bun patch function: this is a nice way for a short-term fix, but this would wreak havoc in the long run…

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?

TransactionSQL does indeed extend the interface of SQL but this is not where the problem lies. It’s because Bun.SQL is directly exposed on the global object Bun while Bun.TransactionSQL is not.

I hope this explains it.

@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

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

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

@luudv do have example