> ## Documentation Index
> Fetch the complete documentation index at: https://docs.valyent.cloud/llms.txt
> Use this file to discover all available pages before exploring further.

# Bun

> Learn how to deploy a Bun application with Hono to Valyent

## Introduction

[Bun](https://bun.sh) is a fast all-in-one JavaScript runtime & toolkit with built-in bundler, test runner, and Node.js-compatible package manager. This guide uses [Hono](https://hono.dev), a lightweight and ultrafast web framework.

## Prerequisites

* a UNIX-like system (see [WSL](https://learn.microsoft.com/en-us/windows/wsl/install) if you are using Windows), in order to use the [Valyent CLI](https://docs.valyent.cloud/cli)
* a Valyent account, with a registered payment method (you can sign up [here](https://console.valyent.cloud/auth/sign-up))
* [Bun](https://bun.sh) installed on your machine

## Installing the CLI

```bash theme={"system"}
curl -L https://cli.valyent.cloud | sh
```

You can inspect the installation script [here](https://cli.valyent.cloud) and the [GitHub repository](https://github.com/valyentdev/cli).

## Authenticating

```bash theme={"system"}
valyent auth login
```

## Initializing the project

First, let's create a new directory and initialize a Bun project:

```bash theme={"system"}
mkdir bun-app && cd bun-app
bun init
```

Install the required dependencies:

```bash theme={"system"}
bun add hono
bun add -d @types/bun
```

Create an `index.ts` file:

```typescript theme={"system"}
import { Hono } from "hono";

const app = new Hono();

app.get("/", (c) => {
  return c.text("Hello Lambda World!");
});

export default app;
```

Now, initialize the `valyent.toml` configuration file:

```bash theme={"system"}
valyent init
```

This command will guide you through selecting or creating a project and an associated application.

## Adding the Dockerfile

Create a Dockerfile:

```bash theme={"system"}
touch Dockerfile
```

Add the following content:

```dockerfile theme={"system"}
# use the official Bun image
FROM oven/bun:1 AS base
WORKDIR /usr/src/app

# install dependencies into temp directory
# this will cache them and speed up future builds
FROM base AS install
RUN mkdir -p /temp/dev
COPY package.json bun.lockb /temp/dev/
RUN cd /temp/dev && bun install --frozen-lockfile

# install with --production (exclude devDependencies)
RUN mkdir -p /temp/prod
COPY package.json bun.lockb /temp/prod/
RUN cd /temp/prod && bun install --frozen-lockfile --production

# copy node_modules from temp directory
# then copy all (non-ignored) project files into the image
FROM base AS prerelease
COPY --from=install /temp/dev/node_modules node_modules
COPY . .

# [optional] tests & build
ENV NODE_ENV=production
RUN bun test
RUN bun run build

# copy production dependencies and source code into final image
FROM base AS release
COPY --from=install /temp/prod/node_modules node_modules
COPY --from=prerelease /usr/src/app/index.ts .
COPY --from=prerelease /usr/src/app/package.json .

# run the app
USER bun
EXPOSE 3000/tcp
ENTRYPOINT [ "bun", "run", "index.ts" ]
```

## Deploy the project

To expose the application’s port, we need to set up a gateway (in this case, the port 3000):

```bash theme={"system"}
valyent gateways create
```

Deploy your application to Valyent:

```bash theme={"system"}
valyent deploy
```

Then, you can type the following:

```bash theme={"system"}
valyent open
```

to open your application in the browser.
