Skip to content
Docs Try Aspire

JavaScriptHostingExtensions Methods

Class Methods 9 members
Provides extension methods for adding JavaScript applications to an Hosting.IDistributedApplicationBuilder.
AddJavaScriptApp(IDistributedApplicationBuilder, string, string, string) Section titled AddJavaScriptApp(IDistributedApplicationBuilder, string, string, string) extension IResourceBuilder<JavaScriptAppResource>
Adds a JavaScript application resource to the distributed application using the specified app directory and run script.
public static class JavaScriptHostingExtensions
{
public static IResourceBuilder<JavaScriptAppResource> AddJavaScriptApp(
this IDistributedApplicationBuilder builder,
string name,
string appDirectory,
string runScriptName = "dev")
{
// ...
}
}
builder IDistributedApplicationBuilder The distributed application builder to which the JavaScript application resource will be added.
name string The unique name of the JavaScript application resource. Cannot be null or empty.
appDirectory string The path to the directory containing the JavaScript application.
runScriptName string optional The name of the npm script to run when starting the application. Defaults to "dev". Cannot be null or empty.
IResourceBuilder<JavaScriptAppResource> A resource builder for the newly added JavaScript application resource.
If a Dockerfile does not exist in the application's directory, one will be generated automatically when publishing. The method configures the resource with Node.js defaults and sets up npm integration.
AddNodeApp(IDistributedApplicationBuilder, string, string, string) Section titled AddNodeApp(IDistributedApplicationBuilder, string, string, string) extension IResourceBuilder<NodeAppResource>
Adds a node application to the application model. Node should be available on the PATH.
public static class JavaScriptHostingExtensions
{
public static IResourceBuilder<NodeAppResource> AddNodeApp(
this IDistributedApplicationBuilder builder,
string name,
string appDirectory,
string scriptPath)
{
// ...
}
}
builder IDistributedApplicationBuilder The Hosting.IDistributedApplicationBuilder to add the resource to.
name string The name of the resource.
appDirectory string The path to the directory containing the node application.
scriptPath string The path to the script relative to the app directory to run.
IResourceBuilder<NodeAppResource> A reference to the ApplicationModel.IResourceBuilder`1.
This method executes a Node script directly using node script.js. If you want to use a package manager you can add one and configure the install and run scripts using the provided extension methods. If the application directory contains a package.json file, npm will be added as the default package manager.

Add a Node app to the application model using yarn and 'yarn run dev' for running during development:

var builder = DistributedApplication.CreateBuilder(args);
builder.AddNodeApp("frontend", "../frontend", "app.js")
.WithYarn()
.WithRunScript("dev");
builder.Build().Run();
AddViteApp(IDistributedApplicationBuilder, string, string, string) Section titled AddViteApp(IDistributedApplicationBuilder, string, string, string) extension IResourceBuilder<ViteAppResource>
Adds a Vite app to the distributed application builder.
public static class JavaScriptHostingExtensions
{
public static IResourceBuilder<ViteAppResource> AddViteApp(
this IDistributedApplicationBuilder builder,
string name,
string appDirectory,
string runScriptName = "dev")
{
// ...
}
}
builder IDistributedApplicationBuilder The Hosting.IDistributedApplicationBuilder to add the resource to.
name string The name of the Vite app.
appDirectory string The path to the directory containing the Vite app.
runScriptName string optional The name of the script that runs the Vite app. Defaults to "dev".
IResourceBuilder<ViteAppResource> A reference to the ApplicationModel.IResourceBuilder`1.
The following example creates a Vite app using npm as the package manager.
var builder = DistributedApplication.CreateBuilder(args);
builder.AddViteApp("frontend", "./frontend");
builder.Build().Run();
WithBuildScript(IResourceBuilder<TResource>, string, string[]?) Section titled WithBuildScript(IResourceBuilder<TResource>, string, string[]?) extension IResourceBuilder<TResource>
Adds a build script annotation to the resource builder using the specified command-line arguments.
public static class JavaScriptHostingExtensions
{
public static IResourceBuilder<TResource> WithBuildScript<TResource>(
this IResourceBuilder<TResource> resource,
string scriptName,
string[]? args = null)
{
// ...
}
}
resource IResourceBuilder<TResource> The resource builder to which the build script annotation will be added.
scriptName string The name of the script to be executed when the resource is built.
args string[]? optional An array of command-line arguments to use for the build script.
IResourceBuilder<TResource> The same resource builder instance with the build script annotation applied.
Use this method to specify custom build scripts for JavaScript application resources during deployment.
WithNpm(IResourceBuilder<TResource>, bool, string?, string[]?) Section titled WithNpm(IResourceBuilder<TResource>, bool, string?, string[]?) extension IResourceBuilder<TResource>
Configures the Node.js resource to use npm as the package manager and optionally installs packages before the application starts.
public static class JavaScriptHostingExtensions
{
public static IResourceBuilder<TResource> WithNpm<TResource>(
this IResourceBuilder<TResource> resource,
bool install = true,
string? installCommand = null,
string[]? installArgs = null)
{
// ...
}
}
resource IResourceBuilder<TResource> The NodeAppResource.
install bool optional When true (default), automatically installs packages before the application starts. When false, only sets the package manager annotation without creating an installer resource.
installCommand string? optional The install command itself passed to npm to install dependencies.
installArgs string[]? optional The command-line arguments passed to npm to install dependencies.
IResourceBuilder<TResource> A reference to the ApplicationModel.IResourceBuilder`1.
WithPnpm(IResourceBuilder<TResource>, bool, string[]?) Section titled WithPnpm(IResourceBuilder<TResource>, bool, string[]?) extension IResourceBuilder<TResource>
Configures the Node.js resource to use pnmp as the package manager and optionally installs packages before the application starts.
public static class JavaScriptHostingExtensions
{
public static IResourceBuilder<TResource> WithPnpm<TResource>(
this IResourceBuilder<TResource> resource,
bool install = true,
string[]? installArgs = null)
{
// ...
}
}
resource IResourceBuilder<TResource> The NodeAppResource.
install bool optional When true (default), automatically installs packages before the application starts. When false, only sets the package manager annotation without creating an installer resource.
installArgs string[]? optional The command-line arguments passed to "pnpm install".
IResourceBuilder<TResource> A reference to the ApplicationModel.IResourceBuilder`1.
WithRunScript(IResourceBuilder<TResource>, string, string[]?) Section titled WithRunScript(IResourceBuilder<TResource>, string, string[]?) extension IResourceBuilder<TResource>
Adds a run script annotation to the specified JavaScript application resource builder, specifying the script to execute and its arguments during run mode.
public static class JavaScriptHostingExtensions
{
public static IResourceBuilder<TResource> WithRunScript<TResource>(
this IResourceBuilder<TResource> resource,
string scriptName,
string[]? args = null)
{
// ...
}
}
resource IResourceBuilder<TResource> The resource builder to which the run script annotation will be added.
scriptName string The name of the script to be executed when the resource is run.
args string[]? optional An array of arguments to pass to the script.
IResourceBuilder<TResource> The same resource builder instance with the run script annotation applied, enabling further configuration.
Use this method to specify a custom script and its arguments that should be executed when the resource is executed in RunMode.
WithViteConfig(IResourceBuilder<ViteAppResource>, string) Section titled WithViteConfig(IResourceBuilder<ViteAppResource>, string) extension IResourceBuilder<ViteAppResource>
Configures the Vite app to use the specified Vite configuration file instead of the default resolution behavior.
public static class JavaScriptHostingExtensions
{
public static IResourceBuilder<ViteAppResource> WithViteConfig(
this IResourceBuilder<ViteAppResource> builder,
string configPath)
{
// ...
}
}
builder IResourceBuilder<ViteAppResource> The resource builder.
configPath string The path to the Vite configuration file. Relative to the Vite service project root.
IResourceBuilder<ViteAppResource> The resource builder.
Use this method to specify a specific Vite configuration file if you need to override the default Vite configuration resolution behavior.

Use a custom Vite configuration file:

var builder = DistributedApplication.CreateBuilder(args);
var viteApp = builder.AddViteApp("frontend", "./frontend")
.WithViteConfig("./vite.production.config.js");
WithYarn(IResourceBuilder<TResource>, bool, string[]?) Section titled WithYarn(IResourceBuilder<TResource>, bool, string[]?) extension IResourceBuilder<TResource>
Configures the Node.js resource to use yarn as the package manager and optionally installs packages before the application starts.
public static class JavaScriptHostingExtensions
{
public static IResourceBuilder<TResource> WithYarn<TResource>(
this IResourceBuilder<TResource> resource,
bool install = true,
string[]? installArgs = null)
{
// ...
}
}
resource IResourceBuilder<TResource> The NodeAppResource.
install bool optional When true (default), automatically installs packages before the application starts. When false, only sets the package manager annotation without creating an installer resource.
installArgs string[]? optional The command-line arguments passed to "yarn install".
IResourceBuilder<TResource> A reference to the ApplicationModel.IResourceBuilder`1.