TypeScript AppHost project structure
Это содержимое пока не доступно на вашем языке.
When you create a TypeScript AppHost with aspire new or aspire init --language typescript, the CLI scaffolds a project with the following structure:
Директорияmy-apphost/
Директория.modules/ Generated TypeScript SDK (do not edit)
- aspire.ts
- base.ts
- transport.ts
- apphost.ts Your AppHost entry point
- aspire.config.json Aspire configuration
- package.json
- tsconfig.json
aspire.config.json
Section titled “aspire.config.json”The aspire.config.json file is the central configuration for your AppHost. It replaces the older .aspire/settings.json and apphost.run.json files.
{ "appHost": { "path": "apphost.ts", "language": "typescript/nodejs" }, "packages": { "Aspire.Hosting.JavaScript": "13.3.0" }, "profiles": { "https": { "applicationUrl": "https://localhost:17127;http://localhost:15118", "environmentVariables": { "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21169", "ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22260" } } }}Key sections
Section titled “Key sections”| Section | Description |
|---|---|
appHost.path | Path to your AppHost entry point (apphost.ts) |
appHost.language | Language runtime (typescript/nodejs) |
packages | Hosting integration packages and their versions. Added automatically by aspire add. |
profiles | Launch profiles with dashboard URLs and environment variables |
Adding integrations
Section titled “Adding integrations”When you run aspire add, the CLI adds the package to the packages section and regenerates the TypeScript SDK:
aspire add redisThis updates aspire.config.json:
{ "packages": { "Aspire.Hosting.JavaScript": "13.3.0", "Aspire.Hosting.Redis": "13.3.0" }}Project references for local development
Section titled “Project references for local development”You can reference a local hosting integration project by using a .csproj path instead of a version:
{ "packages": { "MyIntegration": "../src/MyIntegration/MyIntegration.csproj" }}See Multi-language integrations for details on building hosting integrations that work with TypeScript AppHosts.
.modules/ directory
Section titled “.modules/ directory”The .modules/ directory contains the generated TypeScript SDK. It’s created and updated automatically by the Aspire CLI — do not edit these files.
| File | Purpose |
|---|---|
aspire.ts | Generated typed API for all your installed integrations |
base.ts | Base types and handle infrastructure |
transport.ts | JSON-RPC transport layer |
The SDK regenerates when:
- You run
aspire addto add or update an integration - You run
aspire runoraspire startand the package list has changed - You run
aspire restoreto manually regenerate
Your apphost.ts imports from this SDK:
import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.modules/aspire.js';apphost.ts
Section titled “apphost.ts”The entry point for your AppHost. This is where you define your application’s resources and their relationships:
import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.modules/aspire.js';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
const const cache: RedisResource
cache = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addRedis(name: string, options?: { port?: number; password?: string | ParameterResource;}): RedisResource (+1 overload)
Adds a Redis container resource
addRedis("cache");
const const api: NodeAppResource
api = await const builder: IDistributedApplicationBuilder
builder .IDistributedApplicationBuilder.addNodeApp(name: string, appDirectory: string, scriptPath: string): NodeAppResource
Adds a Node.js application resource
addNodeApp("api", "./api", "src/index.ts") .ExecutableResource.withHttpEndpoint(options?: { port?: number; targetPort?: number; name?: string; env?: string; isProxied?: boolean;} | undefined): NodeAppResource (+1 overload)
Adds an HTTP endpoint
withHttpEndpoint({ env?: string | undefined
env: "PORT" }) .ExecutableResource.withReference(source: EndpointReference | string | uri, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): NodeAppResource (+2 overloads)
Adds a reference to another resource
withReference(const cache: RedisResource
cache);
await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.build(): DistributedApplication
Builds the distributed application
build().DistributedApplication.run(cancellationToken?: cancellationToken): void
Runs the distributed application
run();Package managers
Section titled “Package managers”The Aspire CLI automatically detects which package manager your TypeScript AppHost uses by inspecting lock files and the packageManager field in package.json. The following package managers are supported:
| Package manager | Lock file detected | Notes |
|---|---|---|
| npm | package-lock.json | Default; no extra setup required |
| pnpm | pnpm-lock.yaml | |
| Yarn (v4+) | yarn.lock | Must be Yarn 4 or later (Berry) |
| Bun | bun.lock / bun.lockb |
package.json
Section titled “package.json”The scaffolded package.json includes convenience scripts and the required Node.js version:
{ "name": "my-apphost", "private": true, "type": "module", "scripts": { "dev": "aspire run", "build": "tsc", "lint": "eslint apphost.ts" }, "engines": { "node": "^20.19.0 || ^22.13.0 || >=24" }}The dev script means you can also start your AppHost with npm run dev (or the equivalent for your toolchain, for example bun run dev or pnpm run dev).
Package manager toolchain
Section titled “Package manager toolchain”The Aspire CLI automatically detects which Node-compatible package manager your project uses and adjusts install and run commands accordingly. The following toolchains are supported: npm (default), Bun, Yarn, and pnpm.
Toolchain detection
Section titled “Toolchain detection”The CLI resolves the toolchain by inspecting the AppHost directory and its parent directories (up to eight levels). It checks, in order:
- The
packageManagerfield inpackage.json— for example,"packageManager": "pnpm@9.0.0". - Lockfiles:
bun.lockorbun.lockb→ Bun;pnpm-lock.yaml→ pnpm;yarn.lock,.yarnrc.yml, or a.yarn/directory → Yarn. - If nothing is found, npm is used as the default.
The search walks up parent directories, so a workspace-level packageManager setting or lockfile is picked up automatically by nested AppHosts.
Declaring your toolchain
Section titled “Declaring your toolchain”The recommended way to pin the toolchain is with the packageManager field in package.json, which Aspire uses for toolchain detection. This field is also used by Node.js Corepack for package managers such as Yarn and pnpm:
No extra configuration is required — npm is the default.
{ "packageManager": "pnpm@9.0.0", "name": "my-apphost", "private": true, "type": "module"}{ "packageManager": "yarn@4.0.0", "name": "my-apphost", "private": true, "type": "module"}{ "packageManager": "bun@1.1.0", "name": "my-apphost", "private": true, "type": "module"}Alternatively, committing a toolchain-specific lockfile (pnpm-lock.yaml, yarn.lock, bun.lock, etc.) is sufficient for detection.
Install and run commands by toolchain
Section titled “Install and run commands by toolchain”When a non-npm toolchain is detected, the CLI substitutes the matching commands:
| Toolchain | Install command | Execute command | Watch command |
|---|---|---|---|
| npm | npm install | npx tsx ... | npx nodemon ... |
| pnpm | pnpm install | pnpm exec tsx ... | pnpm exec nodemon ... |
| Yarn | yarn install | yarn exec tsx ... | yarn exec nodemon ... |
| Bun | bun install | bun run {appHostFile} | bun --watch run {appHostFile} |
aspire doctor checks
Section titled “aspire doctor checks”The aspire doctor command checks that the required JavaScript toolchain executable is available. If Bun, Yarn, or pnpm is detected but not installed, the command reports an error with install guidance.
aspire doctorTypeScript validation before startup
Section titled “TypeScript validation before startup”Before starting a TypeScript AppHost, the Aspire CLI runs tsc --noEmit to check for type errors to prevent the dashboard and resources from starting in a partially broken state. If your AppHost has TypeScript compile errors, aspire run and aspire publish stop before the AppHost launches and display the diagnostic output:
apphost.ts(22,7): error TS2322: Type 'string' is not assignable to type 'number'.Watch mode behavior
Section titled “Watch mode behavior”When you use aspire run in watch mode, the TypeScript validation is embedded in the nodemon restart command. The watcher can still start even if there are initial type errors — it will recover automatically as you edit and save files that fix the errors.