Ir al contenido

Node.js hosting extensions

Esta página aún no está disponible en tu idioma.

⭐ Community Toolkit Node.js logo

The Aspire Community Toolkit Node.js hosting extensions package provides extra functionality to the Aspire.Hosting.NodeJS hosting package.

This package provides the following features:

  • Running Vite applications
  • Running Node.js applications using Yarn and pnpm
  • Ensuring that packages are installed before running the application (using the specified package manager)

To get started with the Aspire Community Toolkit Node.js hosting extensions, install the CommunityToolkit.Aspire.Hosting.NodeJS.Extensions NuGet package in the app host project.

Aspire CLI — Añadir paquete CommunityToolkit.Aspire.Hosting.NodeJS.Extensions
aspire add communitytoolkit-nodejs-extensions

La CLI de Aspire es interactiva; asegúrate de seleccionar el resultado adecuado cuando se te pida:

Aspire CLI — Ejemplo de salida
Select an integration to add:
> communitytoolkit-nodejs-extensions (CommunityToolkit.Aspire.Hosting.NodeJS.Extensions)
> Other results listed as selectable options...

This integration extension adds support for running Node.js applications using Yarn or pnpm as the package manager.

To add a Node.js application using Yarn, use the AddYarnApp extension method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var yarnApp = builder.AddYarnApp("yarn-demo")
.WithExternalHttpEndpoints();
builder.AddProject<Projects.ExampleProject>()
.WithReference(yarnApp);
// After adding all resources, run the app...

To add a Node.js application using pnpm, use the AddPnpmApp extension method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var pnpmApp = builder.AddPnpmApp("pnpm-demo")
.WithExternalHttpEndpoints();
builder.AddProject<Projects.ExampleProject>()
.WithReference(pnpmApp);
// After adding all resources, run the app...

This integration extension adds support for running the development server for Vite applications. By default, it uses the npm package manager to launch, but this can be overridden with the packageManager argument.

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var viteApp = builder.AddViteApp("vite-demo")
.WithExternalHttpEndpoints();
// After adding all resources, run the app...
C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var viteApp = builder.AddViteApp("yarn-demo", packageManager: "yarn")
.WithExternalHttpEndpoints();
// After adding all resources, run the app...
C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var viteApp = builder.AddViteApp("pnpm-demo", packageManager: "pnpm")
.WithExternalHttpEndpoints();
// After adding all resources, run the app...

When using the WithNpmPackageInstallation, WithYarnPackageInstallation, or WithPnpmPackageInstallation methods, the package manager is used to install the packages before starting the application. These methods are useful to ensure that packages are installed before the application starts, similar to how a C# application would restore NuGet packages before running.

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var nodeApp = builder.AddNodeApp("node-demo", "server.js")
.WithNpmPackageInstallation();
// After adding all resources, run the app...
C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var yarnApp = builder.AddYarnApp("yarn-demo")
.WithYarnPackageInstallation();
// After adding all resources, run the app...
C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var pnpmApp = builder.AddPnpmApp("pnpm-demo")
.WithPnpmPackageInstallation();
// After adding all resources, run the app...
Preguntas y respuestasColaboraComunidadDebatirVer bajo demanda