Learn Technology in Simple English
Bun is a fast JavaScript runtime and development toolkit designed for modern JavaScript and TypeScript applications.
Unlike tools that focus on only one part of development, Bun combines several important tools into one executable.
Modern JavaScript projects often require several separate tools. For example, a developer may use Node.js as a runtime, npm as a package manager, Jest as a test runner and another tool for bundling.
Bun attempts to provide many of these capabilities through one integrated toolkit.
A runtime allows JavaScript code to execute. Node.js is a well-known JavaScript runtime.
Bun also provides a runtime, but it goes further by including package management, testing and bundling capabilities.
| Tool | Purpose |
|---|---|
| Bun Runtime | Runs JavaScript and TypeScript |
| bun install | Installs dependencies |
| bun test | Runs tests |
| bun build | Builds and bundles applications |
| Bun.serve() | Creates HTTP servers |
The Bun runtime executes JavaScript and TypeScript outside the browser.
For example, create a file named:
hello.ts
Add:
console.log("Hello from Bun!");
Then run:
bun run hello.ts
Bun can execute TypeScript directly without requiring a separate TypeScript execution tool for this basic workflow.
Bun provides installation instructions for major operating systems.
On supported Unix-like systems, the official installation command is:
curl -fsSL https://bun.sh/install | bash
After installation, check the version:
bun --version
Bun can initialize a new project with:
bun init
The command creates files that help you start a JavaScript or TypeScript project.
You can then add your application code and dependencies.
Bun includes its own package manager.
The main installation command is:
bun install
It reads your project's package information and installs dependencies.
Bun is designed to be compatible with the Node.js package ecosystem,
including existing package.json projects.
You can install a package with:
bun add express
This adds Express as a project dependency.
For a development dependency:
bun add --dev typescript
To remove a package:
bun remove express
This updates your project dependencies.
Suppose your package.json contains:
{
"scripts": {
"dev": "node server.js"
}
}
You can run the script with:
bun run dev
Bun can work with scripts already used by many Node.js projects.
bunx is used to execute package binaries.
For example:
bunx create-vite
It is similar in purpose to tools such as npx.
TypeScript is an important part of modern web development.
Bun can run TypeScript files directly in many development workflows.
const name: string = "TechLearn Hub"; console.log(name);
Run it with:
bun run index.ts
Normal JavaScript works with Bun too.
const numbers = [1, 2, 3, 4, 5]; const total = numbers.reduce( (sum, number) => sum + number, 0 ); console.log(total);
Run:
bun run index.js
Bun supports JSX as part of its JavaScript and frontend tooling.
function App() {
return (
<h1>Hello Bun</h1>
);
}
export default App;
This makes Bun useful in React-based workflows.
Bun can be used with React projects.
A React project can use Bun for dependency installation, development scripts, testing and building.
Bun also provides commands for creating React projects through its project initialization workflow.
Bun can serve frontend applications during development.
For example, a project containing an HTML entry point can be served through Bun's development tooling.
bun ./index.html
Modern Bun versions support development features such as hot reloading for frontend workflows.
Bun provides a built-in bundler through Bun.build().
The bundler can process JavaScript, TypeScript, CSS, HTML and other supported files.
A basic example looks like this:
const result = await Bun.build({
entrypoints: ["./src/index.ts"],
outdir: "./dist"
});
if (!result.success) {
console.error(result.logs);
}
You can also use the command-line build tool.
bun build ./src/index.ts
For a production-style build:
bun build ./src/index.ts --production
The exact output can be controlled with build options.
Bun's bundler supports JSX and TypeScript workflows.
bun build ./src/index.tsx --outdir ./dist
The resulting files can then be used as part of a deployment workflow.
Tree shaking removes code that is not needed by the final application when the build system can safely determine that it is unused.
This can help reduce the amount of JavaScript shipped to users.
Code splitting divides an application into smaller pieces.
Instead of loading every piece of JavaScript immediately, parts of the application can be loaded when they are needed.
This is particularly useful for larger web applications.
Bun includes a built-in test runner.
The command is:
bun test
Bun's test runner supports TypeScript and JSX, mocks, snapshots, DOM testing and other testing features.
Create:
math.test.ts
Then:
import { test, expect } from "bun:test";
test("addition works", () => {
expect(2 + 3).toBe(5);
});
Run:
bun test
Bun's official documentation describes bun:test as a
Jest-compatible built-in test runner.
Bun provides an HTTP server API through Bun.serve().
const server = Bun.serve({
port: 3000,
fetch(request) {
return new Response("Hello from Bun!");
}
});
console.log(server.url);
This creates a simple HTTP server.
Modern Bun server applications can define routes.
const server = Bun.serve({
port: 3000,
routes: {
"/": new Response("Home"),
"/about": new Response("About"),
"/api": Response.json({
message: "API is working"
})
}
});
console.log(server.url);
Routes can return normal responses or JSON responses.
Bun includes WebSocket server capabilities.
WebSockets allow applications to maintain a persistent connection between a client and server.
They are useful for:
Bun includes APIs and integrations for working with data stores.
Depending on the application, developers can work with technologies such as:
Always check current Bun documentation for the exact API and support available for your chosen database.
SQLite is a lightweight relational database that is useful for many small applications.
Bun provides built-in database capabilities that can simplify database development.
A database can be useful for:
PostgreSQL is a powerful relational database commonly used in production applications.
Bun provides a built-in SQL client for PostgreSQL and other supported database workflows.
Redis is an in-memory data store commonly used for caching, sessions, queues and fast application data.
Bun includes Redis-related capabilities in its toolkit.
Bun includes an S3 API for working with S3-compatible object storage.
Object storage can be used for:
Applications often need configuration values such as API keys, database URLs and application secrets.
Environment variables are commonly used for this purpose.
const apiKey = process.env.API_KEY; console.log(apiKey);
Bun provides a shell API through $.
import { $ } from "bun";
await $`echo Hello`;
This can be useful for development scripts and automation.
Bun can compile applications into standalone executables.
For example:
bun build ./cli.ts \ --compile \ --outfile todo
The compiled executable can include the application and Bun runtime.
Bun also provides options for cross-compilation and deployment workflows.
Bun is designed with compatibility with the Node.js ecosystem in mind.
Many existing Node.js projects can be used with Bun with little or no modification, although compatibility is not identical for every API or package.
| Feature | Bun | Node.js |
|---|---|---|
| JavaScript runtime | Yes | Yes |
| TypeScript execution | Built in | Usually requires tooling |
| Package manager | Built in | npm commonly used |
| Test runner | Built in | Built-in node:test |
| Bundler | Built in | External tools commonly used |
npm is primarily a package manager and is widely used in the JavaScript ecosystem.
Bun includes a package manager as part of its larger toolkit.
| Feature | Bun | npm |
|---|---|---|
| Package installation | Yes | Yes |
| JavaScript runtime | Yes | No |
| Built-in test runner | Yes | No equivalent as its core purpose |
| Bundler | Yes | Not its core purpose |
| Feature | Bun | Deno |
|---|---|---|
| JavaScript runtime | Yes | Yes |
| TypeScript support | Yes | Yes |
| Package management | Built in | Built in |
| Bundler | Built in | Tooling included |
| Node compatibility | Strong compatibility goal | Compatibility layer available |
These tools have different designs and should be selected according to the needs of the project.
Bun and Vite overlap in some frontend development tasks but they are not identical tools.
Vite is primarily known as a frontend development tool and build system, while Bun is an all-in-one JavaScript runtime and toolkit.
Webpack is a highly configurable JavaScript bundler.
Bun provides bundling as one part of a much larger toolkit.
A developer choosing between them should consider the existing project, ecosystem, configuration requirements and framework support.
my-app/ │ ├── src/ │ ├── index.ts │ └── server.ts │ ├── tests/ │ └── math.test.ts │ ├── package.json ├── bun.lock └── tsconfig.json
The exact structure can change depending on the framework and project.
{
"name": "my-bun-app",
"scripts": {
"dev": "bun run server.ts",
"test": "bun test",
"build": "bun build ./src/index.ts --outdir ./dist"
}
}
You can run:
bun run dev
Tests:
bun test
Build:
bun run build
Here is a simple example of an HTTP API:
const server = Bun.serve({
port: 3000,
routes: {
"/": new Response("Welcome"),
"/api/users": Response.json([
{
id: 1,
name: "Ali"
},
{
id: 2,
name: "Umar"
}
])
}
});
console.log(
`Server running at ${server.url}`
);
This demonstrates how Bun can handle both the server and application logic.
import {
test,
expect
} from "bun:test";
function multiply(a, b) {
return a * b;
}
test(
"multiplication works",
() => {
expect(
multiply(4, 5)
).toBe(20);
}
);
Run:
bun test
A simple frontend project can contain:
index.html src/ app.tsx styles.css package.json
Bun can participate in the development and build workflow for JavaScript, TypeScript, JSX, CSS and HTML.
Hot reloading means that changes to source files can be reflected quickly during development.
This creates a faster feedback loop because developers do not have to manually rebuild the entire application after every small change.
Before deploying an application, developers normally create a production build.
bun build ./index.html --production
Production builds can apply optimizations such as minification, tree shaking and code splitting when supported by the build workflow.
| Command | Purpose |
|---|---|
bun --version |
Show Bun version |
bun init |
Create a project |
bun install |
Install dependencies |
bun add package |
Add a dependency |
bun remove package |
Remove a dependency |
bun run file.ts |
Run a file |
bun run dev |
Run a package script |
bunx package |
Run a package binary |
bun test |
Run tests |
bun build |
Build and bundle |
| Concept | Meaning |
|---|---|
| Bun | JavaScript runtime and toolkit |
| bun install | Install dependencies |
| bun add | Add a package |
| bun run | Run files and scripts |
| bunx | Run package binaries |
| bun test | Run tests |
| bun build | Bundle application code |
| Bun.serve() | Create an HTTP server |
| Bun.build() | Programmatic build API |
| bun:test | Built-in testing module |
No. Bun is a JavaScript runtime and development toolkit.
Bun can be used as a JavaScript runtime and aims for strong Node.js compatibility, but it is not identical to Node.js.
Yes. Bun supports TypeScript execution as part of its runtime.
Yes. Bun's package manager is designed to work with the npm ecosystem.
Yes. Bun includes frontend development and bundling capabilities.
Yes. Bun includes the built-in bun:test test runner.
Yes. Bun provides Bun.serve() for HTTP server applications.
Yes. Bun can be used with React projects and modern frontend workflows.
Yes. Bun includes WebSocket server functionality.
Bun is open-source software and can be used as a development tool.
Bun is an important modern JavaScript technology because it combines several development tools into one toolkit.
Instead of separately managing a runtime, package manager, test runner and bundler, developers can use Bun for many of these tasks.
Bun is especially interesting for developers working with JavaScript, TypeScript, React, APIs, databases and modern web applications.