Guide: Hot Reload in Bun JS (Watch Code for Changes)

Bun incudes 2 very handy flags that will watch your project for file changes.

1. –watch
This is used in the command to run your server, eg:

bun --watch index.ts

If a files contents change within your project folder then bun will cold reload your project – equivalent to restarting the server. All states and connections will be destroyed.

2. –hot

bun --hot index.ts

Again, if files change then bun will reload your project but only the code directly affected. This keeps state and connections open. Obviously if the changed code affects state or connection then you may have very strange debug issues!

Personally I like to just use –watch. I have very very few instances where it would save my time to keep state or connections. However, I could see the use for it on a production server so there was a minimum of user disruption.

Leave a Comment