Node.js hosting extensions

⭐ Community Toolkit

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

[!CAUTION] Package rename In Aspire 13.0, Aspire.Hosting.NodeJs was renamed to Aspire.Hosting.JavaScript.

Use Aspire.Hosting.JavaScript for Aspire 13+ applications. Aspire.Hosting.NodeJs is the old package name.

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)

Hosting integration

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.

Install the NuGet package
dotnet add package CommunityToolkit.Aspire.Hosting.NodeJS.Extensions

Run specific package managers

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

Yarn

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

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...

pnpm

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

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...

Run Vite apps

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.

Using npm (default)

var builder = DistributedApplication.CreateBuilder(args);
  
var viteApp = builder.AddViteApp("vite-demo")
                     .WithExternalHttpEndpoints();
  
// After adding all resources, run the app...

Using Yarn

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

Using pnpm

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

Install packages

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.

npm

var builder = DistributedApplication.CreateBuilder(args);
  
var nodeApp = builder.AddNodeApp("node-demo", "server.js")
                     .WithNpmPackageInstallation();
  
// After adding all resources, run the app...

Yarn

var builder = DistributedApplication.CreateBuilder(args);
  
var yarnApp = builder.AddYarnApp("yarn-demo")
                     .WithYarnPackageInstallation();
  
// After adding all resources, run the app...

pnpm

var builder = DistributedApplication.CreateBuilder(args);
  
var pnpmApp = builder.AddPnpmApp("pnpm-demo")
                     .WithPnpmPackageInstallation();
  
// After adding all resources, run the app...

See also