> For the complete documentation index, see [llms.txt](https://www.ankitavirani.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.ankitavirani.com/experience/packege-mangers/bun.md).

# BUN

## BUN - The Modern JavaScript Runtime 🚀

Bun is a fast all-in-one JavaScript runtime, bundler, transpiler, and package manager. It's designed as a drop-in replacement for Node.js, offering significant performance improvements and a streamlined developer experience.

### Architecture and Concepts 🏗️

Bun's architecture is built for speed and efficiency:

* ⚡ **JavaScriptCore Engine:** Uses Apple's JavaScriptCore for faster execution
* 🔄 **Built-in Bundler:** Native bundling capabilities for quicker builds
* 📦 **Package Manager:** Integrated package management with npm compatibility
* 🔧 **Native Modules:** Written in Zig for low-level performance
* 🌐 **Web Standard APIs:** Implements standard web APIs for better compatibility

#### Key Concepts:

* 🚀 **Speed:** Significantly faster than Node.js in many operations
* 🔌 **Node.js Compatibility:** Runs most Node.js applications out of the box
* 🛠️ **All-in-One Tool:** Bundler, transpiler, and package manager in one
* 📜 **TypeScript Support:** Native TypeScript and JSX support without additional tools

### Bun Architecture Diagram 📊

<figure><img src="/files/tSsz7cSPzuoRePyidT0R" alt=""><figcaption></figcaption></figure>

### Common Bun Commands 🖥️

#### Installation:

```bash
curl -fsSL <https://bun.sh/install> | bash
```

#### Initialize a new project:

```bash
bun init
```

#### Add a dependency:

```bash
bun add [package-name]
bun add -d [package-name]  # Add as dev dependency
```

#### Remove a dependency:

```bash
bun remove [package-name]
```

#### Install all dependencies:

```bash
bun install
```

#### Run a script:

```bash
bun run [script-name]
```

#### Start a development server:

```bash
bun --hot run index.ts
```

### Code Snippets 💻

#### Example package.json:

```json
{
  "name": "my-bun-project",
  "version": "1.0.0",
  "module": "index.ts",
  "type": "module",
  "dependencies": {
    "express": "^4.17.1"
  },
  "devDependencies": {
    "bun-types": "latest"
  },
  "scripts": {
    "start": "bun run index.ts",
    "dev": "bun --hot run index.ts",
    "build": "bun build ./index.ts --outdir ./dist"
  }
}
```

#### Example TypeScript file (index.ts):

```tsx
import express from 'express';

const app = express();
const port = 3000;

app.get('/', (req, res) => {
  res.send('Hello World from Bun!');
});

app.listen(port, () => {
  console.log(`Server running at <http://localhost>:${port}`);
});
```

#### Running the application:

```bash
bun run start
```

Output:

```jsx
Server running at <http://localhost:3000>
```

### Deployment Commands 🚀

Here are some common deployment commands for Bun:

#### Build for production:

```bash
bun run build
```

#### Run tests:

```bash
bun test
```

#### Start in production mode:

```bash
NODE_ENV=production bun run index.ts
```

Add these scripts to your package.json:

```json
{
  "scripts": {
    "build": "bun build ./index.ts --outdir ./dist",
    "test": "bun test",
    "start:prod": "NODE_ENV=production bun run ./dist/index.js"
  }
}
```

### Conclusion 🎉

Bun is a powerful and efficient JavaScript runtime and toolkit that offers significant performance improvements over traditional Node.js setups. Its all-in-one approach simplifies the development workflow, making it an attractive option for modern JavaScript and TypeScript projects. By leveraging Bun's features and commands, developers can create faster, more efficient applications with ease.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://www.ankitavirani.com/experience/packege-mangers/bun.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
