Bun foundations
Use watch mode and project scripts
Restart a Bun program when files change and give common project commands stable names in package.json.
During development, we don’t want to restart the program after every edit. Bun can watch the files loaded by our application.
Run index.ts in watch mode:
bun --watch index.ts
Change the message and save the file. Bun stops the old process and starts it again.
Notice where --watch appears. Bun flags belong immediately after bun:
bun --watch run dev
If you put the flag at the end, Bun may pass it to the script instead.
Add project scripts
Scripts give the project a shared vocabulary. Open package.json and add these entries:
{
"scripts": {
"dev": "bun --watch index.ts",
"start": "bun index.ts",
"typecheck": "tsc --noEmit"
}
}
Now start development with:
bun run dev
Run the ordinary command with:
bun run start
And check the TypeScript types with:
bun run typecheck
You can omit run for many scripts, but I prefer bun run dev. It makes the intention clear and avoids conflicts with Bun’s built-in commands.
Run bun run without a script name to list the scripts available in the project.
Lesson completed