Overview
Build a minimal HTTP server withBun.serve, run it locally, then evolve it by installing a package.
Prerequisites: Bun installed and available on your
PATH. See installation for setup.1
Step 1
Initialize a new project with It’ll prompt you to pick a template, either This automatically creates a
bun init.terminal
Blank, React, or Library. For this guide, we’ll pick Blank.terminal
my-app directory with a basic Bun app.2
Step 2
Run the You should see a console output saying
index.ts file using bun run index.ts.terminal
"Hello via Bun!".3
Step 3
Replace the contents of
index.tsRun the Visit
index.ts with the following code:index.ts file again using bun run index.ts.terminal
http://localhost:3000 to test the server. You should see a simple page that says "Bun!".Seeing TypeScript errors on Bun?
Seeing TypeScript errors on Bun?
If you used Then add the following to your
bun init, Bun will have automatically installed Bun’s TypeScript declarations and configured your tsconfig.json. If you’re trying out Bun in an existing project, you may see a type error on the Bun global.To fix this, first install @types/bun as a dev dependency.terminal
compilerOptions in tsconfig.json:tsconfig.json
4
Step 4
Install the Update
index.tsRun the Visit
figlet package and its type declarations. Figlet is a utility for converting strings into ASCII art.terminal
index.ts to use figlet in routes.index.ts file again using bun run index.ts.terminal
http://localhost:3000/figlet to test the server. You should see a simple page that says "Bun!" in ASCII art.5
Step 5
Let’s add some HTML. Create a new file called Then, import this file in
index.tsRun the Visit
index.html and add the following code:index.html
index.ts and serve it from the root / route.index.ts file again using bun run index.ts.terminal
http://localhost:3000 to test the server. You should see the static HTML page.