Overview
Build a minimal HTTP server withBun.serve, run it locally, then extend it by installing a package.
Prerequisites: Bun is installed and available in your
PATH. For installation, see Installation.Step 1
Initialize a new project with It will prompt you to select a template—either This will automatically create a
bun init.terminal
Blank, React, or Library. For this guide, we’ll choose Blank.terminal
my-app directory with a basic Bun application.Step 2
Run the You should see
index.ts file with bun run index.ts.terminal
"Hello via Bun!" printed to the console.Step 3
Replace the contents of
index.tsRun the file again with Visit
index.ts with the following code:bun run index.ts.terminal
http://localhost:3000 to test the server. You should see a simple page displaying "Bun!".Seeing TypeScript errors in Bun?
Seeing TypeScript errors in Bun?
If you used Then add the following to your
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
tsconfig.json in the compilerOptions:tsconfig.json
Step 4
Install the Update
index.tsRun the file again with Visit
figlet package and its type declarations. Figlet is a utility that converts strings to ASCII art.terminal
index.ts to use figlet in the routes.bun run index.ts.terminal
http://localhost:3000/figlet to test the server. You should see "Bun!" displayed as ASCII art.Step 5
Let’s add some HTML. Create a new file Then import this file in
index.tsRun the file again with Visit
index.html and add the following code:index.html
index.ts and serve it from the root route /.bun run index.ts.terminal
http://localhost:3000 to test the server. You should see the static HTML page.Run scripts
Bun can also execute"scripts" in package.json. Add the following script:
package.json
bun run start.
terminal
⚡️ Performance —
bun 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
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
Install packages
Bun includes a fast, npm-compatible package manager. To install dependencies:terminal
terminal
terminal
Run tests
Bun includes a fast, Jest-compatible test runner. Create a test file:terminal
Bundle for production
Bun can bundle your code for production withBun.build:
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.