Skip to main content
Bun implements a sophisticated module resolution system compatible with Node.js, enhanced with additional features for modern JavaScript development.

Module Systems

Bun supports both ES Modules (ESM) and CommonJS (CJS):

ES Modules

ES Modules
Features:
  • Static imports and exports
  • Top-level await support
  • Tree-shaking friendly
  • Strict mode by default

CommonJS

CommonJS
Features:
  • Dynamic require() calls
  • Synchronous loading
  • Circular dependency handling
  • exports shorthand

Interoperability

Bun seamlessly bridges ESM and CommonJS:
ESM → CommonJS
CommonJS → ESM
Implementation: src/runtime.zig:167 - CommonJS named exports feature

Resolution Algorithm

Bun’s module resolution follows the Node.js algorithm with enhancements:

Resolution Steps

  1. Built-in modules - Check for node:* and bun:* modules
  2. Relative/absolute paths - Resolve file paths
  3. Package imports - Search node_modules
  4. Extensions - Try .ts, .tsx, .js, .jsx, .mjs, .cjs
  5. Index files - Check index.* files
  6. package.json - Resolve via exports, main, module

Relative Imports

Relative imports

Absolute Imports

Absolute imports

Package Imports

Package imports

Extension Resolution

Bun tries extensions in this order:
  1. Exact match (if extension provided)
  2. .ts
  3. .tsx
  4. .js
  5. .jsx
  6. .mjs
  7. .cjs
  8. .json
Extension resolution
Explicit extensions recommended for clarity and performance.

package.json Fields

Bun respects multiple package.json fields:

exports Field

Modern package entry points:
package.json
Import usage:

Conditional Exports

Conditional exports
Condition priority (Bun target):
  1. bun
  2. node
  3. import / require
  4. default
Implementation: src/options.zig:508-533 - Default conditions per target

main, module, browser

Legacy entry point fields:
Legacy fields
Resolution priority:
  1. exports (if present)
  2. module
  3. main
  4. index.js
Implementation: src/options.zig:455-505 - Main field resolution

type Field

Determines module system:
package.json
package.json
Overrides:
  • .mjs always ESM
  • .cjs always CommonJS

Path Mapping (tsconfig.json)

Configure import aliases:
tsconfig.json
Using path aliases
Implementation: src/resolver/resolver.zig - Path resolution

Built-in Modules

Bun provides built-in modules:

Node.js Compatibility

Node.js modules
All Node.js built-ins available with node: prefix. Implementation: src/options.zig:150-333 - Node.js built-in patterns

Bun-specific Modules

Bun modules
Available modules:
  • bun - Main Bun APIs
  • bun:ffi - Foreign Function Interface
  • bun:sqlite - SQLite database
  • bun:test - Test runner
  • bun:jsc - JavaScriptCore internals
  • bun:wrap - Internal runtime

Import Attributes

Specify how to import modules:
Import attributes
Supported types:
  • json - Parse as JSON
  • text - Load as string
  • file - Copy to output
  • toml - Parse as TOML

Dynamic Imports

Load modules at runtime:
Dynamic imports
Benefits:
  • Code splitting
  • Lazy loading
  • Conditional imports
  • String-based paths

Circular Dependencies

Bun handles circular dependencies:
Circular dependencies
Best practice: Avoid circular dependencies when possible.

Import Maps

Map bare specifiers to URLs:
import-map.json
Using import maps
Import maps are experimental and primarily for browser compatibility.

Resolution Cache

Bun caches module resolution results: Cache location:
  • Resolution cache: In-memory
  • Transpiler cache: Disk (see TypeScript)
Cache invalidation:
  • File system changes (watch mode)
  • package.json modifications
  • node_modules updates

Module Loader Hooks

Customize module loading (advanced):
Module hooks

Preloading

Load modules before main script:
Preload flag
bunfig.toml
Use cases:
  • Environment setup
  • Polyfills
  • Global initialization
  • Instrumentation

Performance

Optimization Tips

  1. Use explicit extensions
  2. Prefer ESM over CommonJS
    • Static analysis
    • Tree-shaking
    • Async loading
  3. Use barrel files sparingly
  4. Leverage dynamic imports for code splitting

Debugging

Trace Resolution

Debug resolution
Print resolution

Resolution Failures

Common errors

Implementation Details

Module resolution implementation:
  • Resolver: src/resolver/resolver.zig - Core resolution logic
  • Module loader: src/bun.js/module_loader/ - Module loading
  • Import record: src/import_record.zig - Import tracking
  • Package.json: src/resolver/package_json.zig - Package metadata