Fix Bun Hot Reload Not Working

Is hot reloading not working in your bun.js project? This article gives you 3 potential fixes (from a real developer).

First of all there are 2 ways you can hot reload code in your project:

  1. bun –hot index.ts
  2. bun –watch index.ts

The first one, –hot, watches your files for changes and only reloads the content. It DOES NOT restart the bun process. Therefore if your new code has some dependancy on the process restarting then it (probably) won’t be picked up.

–hot reloading is great if you’re only working on content and want to keep state in your server across reloads. If you don’t need that then it’s much better to do a cold reload as below:

Fix 1: Use the –watch command instead of –hot

You’re much better off using the –watch command to “hot” reload the app on changes. This behaves more more like the traditional way of hot reloading in node.js, for example when using pm2, nodemon or similar process managers.

Note that each detected change causes the bun process to restart and server state will be lost.

Fix 2: WSL (Linux on Windows) doesn’t hot reload

This is a know bug when using bun.js on Windows Subsystem for Linux. The process somehow doesn’t pick up the changes in files on the Windows file system. See this issue on github: https://github.com/oven-sh/bun/issues/5155

Fix a) The best option is to run bun on native Linux or Mac of course but for some devs that’s not an option due to work place restrictions.

Fix b) Personally I use a Virtual Machine in Virtualbox or VMware and bun runs as expected. Again this may not be an option due to IT restrictions.

Also note: bun is not ready for prime time on Windows just yet (start of 2024) so you may run into other issues that aren’t yet documented.

Fix 3: Use a traditional code watcher

You don’t have to use the bun.js watcher tools. Traditional code watchers and reloaders work well too. In fact, if you’re migrating from node then you probably already have these services setup and don’t particularly want to change them out for the bun alternative.

a) You can use pm2 (my go to) to reload your project: https://www.npmjs.com/package/pm2

b) You can also use nodemon to do the same: https://www.npmjs.com/package/nodemon

Have you had any other issues with bun.js? If so then please leave them in the comments below!

Leave a Comment