Skip to main content

Overview

Build a minimal HTTP server with Bun.serve, run it locally, then extend it by installing a package.
Prerequisites: Bun is installed and available in your PATH. For installation, see Installation.

1

Step 1

Initialize a new project with bun init.
terminal
It will prompt you to select a template—either Blank, React, or Library. For this guide, we’ll choose Blank.
terminal
This will automatically create a my-app directory with a basic Bun application.
2

Step 2

Run the index.ts file with bun run index.ts.
terminal
You should see "Hello via Bun!" printed to the console.
3

Step 3

Replace the contents of index.ts with the following code:
index.ts
Run the file again with bun run index.ts.
terminal
Visit http://localhost:3000 to test the server. You should see a simple page displaying "Bun!".
If you used bun init, Bun automatically installs Bun’s TypeScript declarations and configures your tsconfig.json. If you’re trying Bun in an existing project, you may see type errors for the Bun global.To fix this, first install @types/bun as a dev dependency.
terminal
Then add the following to your tsconfig.json in the compilerOptions:
tsconfig.json
4

Step 4

Install the figlet package and its type declarations. Figlet is a utility that converts strings to ASCII art.
terminal
Update index.ts to use figlet in the routes.
index.ts
Run the file again with bun run index.ts.
terminal
Visit http://localhost:3000/figlet to test the server. You should see "Bun!" displayed as ASCII art.
5

Step 5

Let’s add some HTML. Create a new file index.html and add the following code:
index.html
Then import this file in index.ts and serve it from the root route /.
index.ts
Run the file again with bun run index.ts.
terminal
Visit http://localhost:3000 to test the server. You should see the static HTML page.
🎉 Congratulations! You’ve successfully built a simple HTTP server with Bun and installed a package.

Run scripts

Bun can also execute "scripts" in package.json. Add the following script:
package.json
Then run it with bun run start.
terminal
⚡️ Performancebun run is approximately 28x faster than npm run (6ms vs 170ms of overhead).

Watch mode

Use the --watch flag to automatically restart the process when any imported file changes:
terminal
Now when you edit index.ts, Bun will automatically restart the server.

Hot reloading

For even faster development, use --hot mode, which reloads code without restarting the process:
terminal
This preserves application state between reloads, making it ideal for development.

Install packages

Bun includes a fast, npm-compatible package manager. To install dependencies:
terminal
To add a specific package:
terminal
To add a dev dependency:
terminal

Run tests

Bun includes a fast, Jest-compatible test runner. Create a test file:
index.test.ts
Run your tests:
terminal

Bundle for production

Bun can bundle your code for production with Bun.build:
build.ts
Or use the CLI:
terminal

Next steps

Runtime

Learn about Bun’s JavaScript runtime, including TypeScript support, JSX, and Web APIs.

Package Manager

Explore Bun’s fast package manager with workspaces and global cache.

Test Runner

Write and run tests with Bun’s Jest-compatible test runner.

Bundler

Bundle TypeScript, JSX, and CSS for the browser or server.