bun Verified current stable Not installed? Build Tools

Bun / Bundle With External Dependencies

Bundle With External Dependencies

Bundle JavaScript with external dependencies specified.

$
Terminal
bun build <path/to/entry.ts> --outfile <path/to/output.js> -e <react react-dom>

When To Use

When integrating third-party libraries that should not be bundled into the final artifact for smaller output sizes.

Pro Tip

Consider using a specific version range for external dependencies to avoid compatibility issues.

Terminal Output

Expected runtime feedback

Simulated preview
>
Output
Build started...
Bundling /path/to/entry.ts with external dependencies:
  - react
  - react-dom
Output file created at /path/to/output.js
Build completed successfully!

Anatomy of Output

Understanding the result

Bundling dependencies: [react, react-dom] External Dependencies

Shows which external libraries are being referenced.

Package not found: react-dom Dependency Status

Indicates if any specified external package is missing.

Bundled successfully in 600 ms with external dependencies. Bundle Summary

Completion time for the bundling process.

Troubleshooting

Common pitfalls

Error: Package react is not listed as a dependency

Solution: Add react to your project's dependency list.

Error: Unmet peer dependency: react-dom

Solution: Ensure the compatible version of react is installed.

Error: Unable to locate external dependency 'react'

Solution: Check repository URL or package installation.

Command Breakdown

What each part is doing

bun
Base Command
The executable that performs this operation. Here it runs Bun before the shell applies any redirect operators.
<path/to/entry.ts>
path to entry.ts
The value supplied for path to entry.ts.
<path/to/output.js>
path to output.js
The value supplied for path to output.js.
-e
e| external
The value supplied for e| external.
<react react-dom>
react react dom
The value supplied for react react dom.
--outfile
Command Option
Tool-specific option used by this command invocation.
-e
Command Option
Tool-specific option used by this command invocation.

How To Run

Execution path

  1. Step 1

    Run the command: bun build /path/to/entry.ts --outfile /path/to/output.js --external react react-dom

  2. Step 2

    Check the output file at /path/to/output.js to verify the bundling was successful.

Alternative Approaches

Comparable commands in other tools

Alternative build tools tools for the same job.