> 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.
