Blog / Fix Module not found: Can't resolve 'bun' in Next.JS with Turbopack
2 min read
Fix Module not found: Can't resolve 'bun' in Next.JS with Turbopack

Fix Module not found: Can't resolve 'bun' in Next.JS with Turbopack

Introduction

When working with cutting-edge JavaScript tooling, you’ll occasionally encounter compatibility issues between different technologies. One such challenge is using Turbopack—Next.js’s new bundler—alongside Bun’s innovative APIs. Many developers have reported import errors and unexpected behavior when attempting to use these technologies together.

In this comprehensive guide, I’ll walk you through practical workarounds that will save you hours of debugging and allow you to leverage the best of both tools in your development workflow.

Understanding the Problem

Turbopack, designed to be an incredibly fast replacement for Webpack, sometimes struggles with direct imports from Bun’s module system. This incompatibility typically manifests as module resolution errors during development or build time, creating frustrating roadblocks in your development process.

The core issue lies in how Turbopack processes import statements specifically targeting Bun’s native modules. Let’s explore how to solve this elegantly.

Solution: Use Namespaced Imports

The simplest workaround involves changing how you import and use Bun’s APIs in your code.

Before (Problematic Approach)

import { SQL } from 'bun';

// Later in your code
const client = new SQL();

After (Compatible Approach)

// No direct import needed
const client = Bun.SQL();

This approach leverages Bun’s global namespace, which is automatically available in your Bun runtime environment without requiring explicit imports. This small adjustment maintains full functionality while avoiding the module resolution errors that Turbopack might encounter.

Using third-party libraries.

What happens when the incompatibility exists within third-party libraries that you don’t directly control? Bun offers an elegant solution with its bun patch feature, allowing you to create persistent patches for your dependencies.

Fixing drizzle-orm/bun-sql

A common scenario involves the popular ORM library Drizzle when using its Bun SQL adapter. When running with Turbopack, you might encounter 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";

Step-by-Step Patching Process

  1. Locate the problematic file: Navigate to ./node_modules/drizzle-orm/bun-sql/driver.js in your project directory.

  2. Modify the code:

    • Remove the line import { SQL } from "bun";
    • Replace all instances of new SQL() with new Bun.SQL()
  3. Create a permanent patch: Run the following command in your terminal:

    bun patch --commit 'node_modules/drizzle-orm'
  4. Verify the patch: This command creates two important things:

    • A patch file in your project’s patches directory
    • An entry in the patchedDependencies section of your package.json

The magic happens during future dependency installations—every time you run bun i, your custom fixes are automatically applied, ensuring consistent behavior across environments and team members.

Conclusion

As of now (Mar 7 2025) we await more permanent solutions from the Turbopack and Bun teams (open issue #75220), these workarounds provide a reliable path forward for developers looking to leverage both technologies together.

More

Read other articles

There are no more articles at the moment. Check back later for more content!

Trust us

You have a great idea? Let us help you with it

Write to us and see how we can help your business. You will get a free valuation.

Click & Contact Us