Skip to content
DocsTry Aspire
DocsTry

Bun integration

Bun logo

The Aspire Bun hosting integration enables you to run Bun applications alongside your other Aspire resources in the app host. Bun apps participate in the same service discovery, health checks, OpenTelemetry export, and Aspire dashboard support as the rest of your solution.

To access the Bun hosting APIs in your AppHost project, install the 📦 Aspire.Hosting.JavaScript NuGet package:

Aspire CLI — Add Aspire.Hosting.JavaScript package
aspire add javascript

The Aspire CLI is interactive, be sure to select the appropriate search result when prompted:

Aspire CLI — Example output prompt
Select an integration to add:
> javascript (Aspire.Hosting.JavaScript)
> Other results listed as selectable options...

Migrate from the Community Toolkit package

Section titled “Migrate from the Community Toolkit package”

Remove CommunityToolkit.Aspire.Hosting.Bun and install Aspire.Hosting.JavaScript. The official API requires the app directory and script path separately:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
// Deprecated Toolkit API:
// await builder.addBunApp('bun-api', {
// workingDirectory: '../bun-app',
// entryPoint: 'server.ts',
// });
// Official Aspire.Hosting.JavaScript API:
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addBunApp(name: string, appDirectory: string, scriptPath: string): BunAppResource (+2 overloads)

Adds a Bun application to the application model. Bun should be available on the PATH.

addBunApp
('bun-api', '../bun-app', 'server.ts');
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.build(): DistributedApplication

Builds the distributed application

build
().
DistributedApplication.run(cancellationToken?: cancellationToken): void

Runs the distributed application

run
();

The official package requires both paths. In TypeScript, replace the Toolkit options object with the official appDirectory and scriptPath arguments.

Add a Bun application to your AppHost using AddBunApp / addBunApp:

apphost.mts
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();
const bunApp = await builder.addBunApp('bun-api', '../bun-app', 'server.ts');
await bunApp.withHttpEndpoint({ port: 3000, env: 'PORT' });
await builder.build().run();

AddBunApp requires:

  • name: The name of the resource in the Aspire dashboard.
  • appDirectory: The path to the directory containing your Bun application, relative to the AppHost project.
  • scriptPath: The script to run relative to appDirectory, such as server.ts.

The resource is typed as BunAppResource.

Pass a different scriptPath to run a different script:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
const
const bunApp: BunAppResource
bunApp
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addBunApp(name: string, appDirectory: string, scriptPath: string): BunAppResource (+2 overloads)

Adds a Bun application to the application model. Bun should be available on the PATH.

addBunApp
(
'bun-api',
'../bun-app',
'src/http/server.ts'
);
await
const bunApp: BunAppResource
bunApp
.
ExecutableResource.withHttpEndpoint(options?: {
port?: number;
targetPort?: number;
name?: string;
env?: string;
isProxied?: boolean;
} | undefined): BunAppResource (+1 overload)

Adds an HTTP endpoint

withHttpEndpoint
({
port?: number | undefined
port
: 3000,
env?: string | undefined
env
: 'PORT' });
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.build(): DistributedApplication

Builds the distributed application

build
().
DistributedApplication.run(cancellationToken?: cancellationToken): void

Runs the distributed application

run
();

When your Bun app includes a package.json file, Aspire uses Bun as the package manager and installs packages automatically before the application starts.

Bun applications typically read the port from an environment variable. Use WithHttpEndpoint to declare the HTTP endpoint and bind it to a named environment variable:

apphost.mts
import {
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
} from './.aspire/modules/aspire.mjs';
const
const builder: IDistributedApplicationBuilder
builder
= await
function createBuilder(): IDistributedApplicationBuilder

Creates a new distributed application builder

createBuilder
();
const
const bunApp: BunAppResource
bunApp
= await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.addBunApp(name: string, appDirectory: string, scriptPath: string): BunAppResource (+2 overloads)

Adds a Bun application to the application model. Bun should be available on the PATH.

addBunApp
('bun-api', '../bun-app', 'server.ts');
await
const bunApp: BunAppResource
bunApp
.
ExecutableResource.withHttpEndpoint(options?: {
port?: number;
targetPort?: number;
name?: string;
env?: string;
isProxied?: boolean;
} | undefined): BunAppResource (+1 overload)

Adds an HTTP endpoint

withHttpEndpoint
({
port?: number | undefined
port
: 3000,
env?: string | undefined
env
: 'PORT' });
await
const builder: IDistributedApplicationBuilder
builder
.
IDistributedApplicationBuilder.build(): DistributedApplication

Builds the distributed application

build
().
DistributedApplication.run(cancellationToken?: cancellationToken): void

Runs the distributed application

run
();

Your Bun application reads the PORT variable at startup:

server.ts
const server = Bun.serve({
port: process.env.PORT || 3000,
fetch(request) {
return new Response('Hello from Bun!');
},
});
console.log(`Server listening on port ${server.port}`);