← Debugging Guides

阅读中文版本

Bun — module not found despite being installed

Bun fails to resolve a module that is listed in package.json and present in node_modules, often due to resolution strategy or conditional exports.

intermediatedebuggingbunmodules

Symptoms

  • error: Could not resolve "module-name"
  • The module is in node_modules and package.json but Bun cannot find it
  • Works with Node.js but not Bun

Possible causes

  • The package uses conditional exports that Bun does not support yet
  • The import path uses a subpath not exported in package.json exports field
  • Bun cache is stale after adding the dependency

Diagnosis steps

Verify the module is installed

bun pm ls | grep module-name

Interpretation

If missing, run bun install; if present, the issue is resolution not installation.

Check the package exports field

cat node_modules/module-name/package.json | grep -A20 exports

Interpretation

Confirm the subpath you import is listed in the exports map.

Clear Bun cache

rm -rf node_modules/.cache && bun install

Interpretation

A stale cache can cause resolution failures after dependency changes.

Root cause

Bun uses a different module resolution algorithm than Node.js; packages with conditional exports or non-standard subpaths may not resolve under Bun stricter rules.

Fix

Import only from the package main entry or a subpath explicitly listed in exports; if the package lacks an exports map, use the full path or open an issue with the package author.

Verify the fix

Run the script with bun and confirm the module loads without error, then verify the imported function works as expected.

Symptom walkthrough

Bun fails to resolve a module that is listed in package.json and present in node_modules, often due to resolution strategy or conditional exports. The error appears in production but may not surface in local development because the conditions that trigger it depend on the deployment environment or runtime configuration. Understanding the full symptom set helps distinguish this issue from similar errors.

Diagnosis in practice

Follow the diagnosis steps in the frontmatter to isolate the cause. Each step includes a command and an interpretation of the output. The goal is to narrow down whether the issue is in the configuration, the runtime, or the application code. Reproduce the error in a minimal setup before applying a fix.

Root cause

Bun uses a different module resolution algorithm than Node.js; packages with conditional exports or non-standard subpaths may not resolve under Bun stricter rules.

Fix

Import only from the package main entry or a subpath explicitly listed in exports; if the package lacks an exports map, use the full path or open an issue with the package author. After applying the fix, monitor the system to confirm the error does not recur under load or in different environments.

Verification

Run the script with bun and confirm the module loads without error, then verify the imported function works as expected. If the verification passes, the fix is confirmed; if not, re-examine the diagnosis steps for missed contributing factors.

Diagnostic commands

Run these commands to reproduce and diagnose the issue:

bun pm ls | grep module-name
cat node_modules/module-name/package.json | grep -A20 exports
rm -rf node_modules/.cache && bun install