🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Astro Fundamentals••By Komal Nakrani

Astro with Bun: Install, Run, Build and Deploy

Use Astro with Bun: create a project with bunx, run dev and build, choose between Bun as package manager or runtime, run tests with bun test, and deploy.

#Astro#Astro Bun#Bun#Tooling#Package Managers

You can use Astro with Bun by creating a project with bunx create-astro@latest, installing with bun install, and running bun run dev and bun run build. Used that way, Bun is the package manager and script runner while the astro CLI still runs on Node.js. Add --bun (bun --bun run dev) to run Astro on the Bun runtime itself, which often works but is less tested than Node.js.

This guide covers both setups, what to install, how tests and type checking fit in, and how to deploy an Astro project that uses Bun.

Key Takeaways

  • bunx create-astro@latest, bun install and bun run dev give you Bun’s install speed with no other changes.
  • bun run <script> respects the #!/usr/bin/env node line in the astro binary; bun --bun run <script> forces the Bun runtime.
  • Astro 7 requires Node.js 22.23.4 or newer. Keep Node.js installed even if you use Bun for installs.
  • Static output from astro build needs no runtime, so hosting is the same as any Astro site.
  • Commit bun.lock and nothing else, so hosts detect Bun and install the same versions you tested.

Bun as package manager or as runtime

Bun is two things in one binary: a fast package manager and a JavaScript runtime that aims for Node.js compatibility. With Astro, that gives you two distinct setups:

Bun as package manager Bun as runtime
Command bun run dev bun --bun run dev
Who runs astro Node.js Bun
Install speed Bun Bun
Compatibility risk Very low Some Node.js APIs and native modules behave differently
Needs Node.js installed Yes No for Astro itself, but keep it for fallbacks

The astro binary starts with #!/usr/bin/env node. bun run honors that line and launches Node.js. The --bun flag tells Bun to ignore it and run the script with Bun instead. That flag is the whole difference between the two setups.

Most teams use the first setup: faster installs and CI, nothing else changes. Use the second only after you have tested a full build with your integrations.

Create an Astro project with Bun

Install Bun from bun.sh and check the version, then create the project:

bun --version
bunx create-astro@latest my-site
cd my-site
bun install
bun run dev

The create wizard asks the same questions as with npm. If it already installed dependencies with another package manager, delete node_modules and package-lock.json, then run bun install so you end up with a single bun.lock.

The scripts in package.json don’t change:

{
  "scripts": {
    "dev": "astro dev",
    "build": "astro build",
    "preview": "astro preview",
    "astro": "astro"
  }
}

Run any of them with bun run <name>, and Astro CLI commands with bunx astro <command>:

bunx astro add mdx sitemap
bunx astro check
bunx @astrojs/upgrade

The Astro CLI cheatsheet lists every command; each works the same through bunx.

Run Astro on the Bun runtime

To make Astro, Vite and your integrations run on Bun, add --bun:

bun --bun run dev
bun --bun run build

If that works for your project, you can bake it into the scripts:

{
  "scripts": {
    "dev": "bunx --bun astro dev",
    "build": "bunx --bun astro build",
    "preview": "bunx --bun astro preview"
  }
}

Things to check before relying on it:

  • Native modules. Sharp, which Astro uses for image optimization, ships native binaries. Build a page with <Image /> and confirm the optimized files appear in dist/_astro/.
  • Integrations that spawn processes or use less common Node.js APIs. Run a full build, not just the dev server.
  • Your adapter, if you render routes on demand. The official adapters target Node.js and specific host runtimes; test the built server under Bun before deploying it.

If anything breaks, drop --bun and you are back on Node.js with no other changes.

TypeScript and Bun types

Astro projects already have TypeScript set up through tsconfig.json. If you write scripts that use Bun-specific APIs (Bun.file(), Bun.write(), bun:test), add Bun’s types:

bun add -d @types/bun

Keep Bun-only code in scripts and tests, not in Astro components. Components run inside Vite during dev and build, and if you ever switch back to Node.js, Bun.* calls will fail. The Astro TypeScript config guide covers the rest of the setup.

Test with bun test

Bun includes a Jest-compatible test runner. It’s a good fit for plain TypeScript utilities in an Astro project: slug helpers, date formatting, reading time.

// src/utils/slugify.ts
export function slugify(input: string): string {
  return input
    .toLowerCase()
    .trim()
    .replace(/[^a-z0-9]+/g, '-')
    .replace(/^-|-$/g, '');
}
// src/utils/slugify.test.ts
import { describe, expect, test } from 'bun:test';
import { slugify } from './slugify';

describe('slugify', () => {
  test('lowercases and hyphenates', () => {
    expect(slugify('Hello Astro World')).toBe('hello-astro-world');
  });

  test('trims leading and trailing separators', () => {
    expect(slugify('  --Astro 7--  ')).toBe('astro-7');
  });
});

Run bun test. For tests that render .astro components, use Vitest with Astro’s getViteConfig() and the Container API, because those need Vite’s Astro plugin. The Astro testing tools guide compares the options.

Build and deploy an Astro site that uses Bun

bun run build writes the same dist/ folder as npm or pnpm. For static output, that folder is plain HTML, CSS and JavaScript and needs no runtime, so it deploys anywhere. The static deployment docs cover the per-host settings.

Hosts pick the package manager from your lockfile. Commit bun.lock, remove other lockfiles, and set the build command to bun run build if the host doesn’t detect it. In GitHub Actions, the official oven-sh/setup-bun action installs Bun:

# .github/workflows/build.yml
name: Build
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
      - uses: oven-sh/setup-bun@v2
      - run: bun install --frozen-lockfile
      - run: bun run build

Node.js is installed too, because bun run build runs the astro binary on Node.js. --frozen-lockfile fails the job if bun.lock is out of date, which keeps CI and your machine on the same versions. The GitHub Actions workflows guide adds caching and deploy steps.

For on-demand rendering, build with an adapter. With @astrojs/node in standalone mode, you start the server with node ./dist/server/entry.mjs. Running that file with bun may work, but treat it as something to test, not a default.

Use Bun with an existing Astro project or theme

Switching an existing project takes three commands:

rm -rf node_modules package-lock.json pnpm-lock.yaml
bun install
bun run build

If the build passes, commit bun.lock and the removed lockfiles. Komal Nakrani Astro themes are standard Astro 7 projects with npm scripts, so they install and build the same way with Bun.

When should you not bother? If your team, CI and host are all set up for pnpm or npm and installs aren’t a bottleneck, switching gains little. The npm vs pnpm vs Bun comparison covers the trade-offs, and the official Astro Bun recipe tracks any changes in Astro’s support.

FAQ

Does Astro work with Bun?

Yes. Bun works as a package manager and script runner for Astro with bunx create-astro@latest, bun install and bun run dev. Running Astro on the Bun runtime with --bun also works for many projects, but Node.js remains the runtime Astro is primarily tested on.

Is Astro faster with Bun?

Installs are faster, which helps in CI and on fresh clones. Build time is mostly spent in Vite, image processing and rendering, so switching the runtime to Bun usually changes it far less than switching the installer. Measure your own build with astro build --timings before and after.

Do I still need Node.js if I use Bun with Astro?

For the standard setup, yes. bun run dev runs the astro binary on Node.js, and Astro 7 requires Node.js 22.23.4 or newer. Only the --bun setup runs without Node.js, and keeping Node.js installed gives you a fallback.

How do I add an integration to Astro with Bun?

Run bunx astro add <name>, for example bunx astro add mdx. It installs the package with Bun and updates astro.config.mjs, just as npx astro add does with npm.

Can I deploy an Astro and Bun project to Netlify, Vercel or Cloudflare Pages?

Yes. These hosts detect Bun from a committed bun.lock and run your build command. Static output in dist/ deploys like any Astro site; on-demand routes use the host’s adapter as usual.