bun Verified current stable Not installed? Build Tools

Bun / Watch For File Changes And Rebuild

Watch For File Changes And Rebuild

Watch for changes and rebuild the output JavaScript file automatically.

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

When To Use

During active development to ensure timely recompilation upon file modification.

Pro Tip

Use the `--verbose` flag for detailed output that can help diagnose rebuild performance issues.

Terminal Output

Expected runtime feedback

Simulated preview
>
Output
Watching for changes in path/to/entry.ts...

   File changed: path/to/entry.ts
   Building...

   Success! Output written to path/to/output.js
   Next build will trigger on further changes.

Anatomy of Output

Understanding the result

Bundling /path/to/entry.ts → /path/to/output.js Bundle Path

Indicates which entry file is being bundled into which output.

Watching for file changes... State

Indicates that the process is monitoring for changes.

Successfully built in 500 ms Build Time

Duration to complete the last successful build.

Troubleshooting

Common pitfalls

Error: File not found '/path/to/entry.ts'

Solution: Check the provided path for correctness.

Error: Unable to write to '/path/to/output.js'

Solution: Verify write permissions for the output directory.

Error: Build failed due to syntax error

Solution: Inspect the entry file for syntax issues.

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.
--outfile
Command Option
Tool-specific option used by this command invocation.
--watch
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 --watch`

  2. Step 2

    Make a change in the 'path/to/entry.ts' file.

  3. Step 3

    Observe the rebuild process and check for the success message in the terminal.

Alternative Approaches

Comparable commands in other tools

Alternative build tools tools for the same job.