SalmanShariati
SalmanShariati

Reputation: 3893

ASP.NET Core project with NextJS

I've created an ASP.NET Core 3.1 project with React.js template using Visual Studio 2019

Everything is ok when I run the project as a React.js application. But when I change the application to a NextJS applications, when I run it I gives me the error :

AggregateException: One or more errors occurred.
(One or more errors occurred. (The NPM script 'start' exited without indicating that the create-react-app server was listening for requests.
The error output was: Error: Could not find a valid build in the 'C:\Users\myname\source\repos\Panel\Panel\ClientApp\.next' directory! Try building your app with 'next build' before starting the server.

I can run the app using npm run dev but I want to run the app using visual studio on the same port of ASP.NET Core app is run, like the React.js applications.

How can I add commands like npm run dev and execute them to run the app on the same port?

Upvotes: 1

Views: 2975

Answers (1)

Daniel Mendez
Daniel Mendez

Reputation: 31

You need a reverse proxy in your Startup.cs to point to the script used to start your next.js app. For example:

app.UseSpa(spa =>
{
    spa.Options.SourcePath = "ClientApp/nextjs-client";

    if (env.IsDevelopment())
    {
        spa.Options.StartupTimeout = TimeSpan.FromSeconds(120);
        spa.UseProxyToSpaDevelopmentServer("http://localhost:3000");
        spa.UseReactDevelopmentServer(npmScript: "dev");
    }
});

Upvotes: 3

Related Questions