Deploy asp.net react template without nodejs depen

2020-07-27 06:32发布

问题:

I'm having problems with deploying and running the ReactRedux template on an IIS server which doesn't has nodejs. The app was created using AspNetCore.SpaTemplates' "reactredux" option: https://blogs.msdn.microsoft.com/webdev/2017/02/14/building-single-page-applications-on-asp-net-core-with-javascriptservices/

The app is the same as the template, no changes were made. We first tried to host it on IIS with nodejs installed on the IIS server, everything works fine:

screenshot with nodejs

But when we uninstalled nodejs from the server and restarted IIS, it wasn't working anymore:

screenshot without nodejs

We couldn't figure out what the error is because running it in development mode requires nodejs and when we run it with dotnet run in development mode the error message complains about node.js not being installed, as expected.

We tried to deploy the app running:

$Env:ASPNETCORE_ENVIRONMENT = "Production"    
dotnet publish -c Release

and using Visual Studios build>publish interface.

Acording to the docs here, the app shouldn't need nodejs when its deployed for production:

https://docs.microsoft.com/en-us/aspnet/core/spa/react?tabs=visual-studio

Unlike the development build, the production build doesn't require Node.js to be installed on the server.

I'm new to asp.net and deploying apps on IIS, maybe the solution is trivial but we couldn't manage to solve this, thanks in advance.

回答1:

If you don't want any nodejs dependency, make sure you disabled server-side prerendering: https://github.com/aspnet/JavaScriptServices/issues/932

If you're not doing server-side prerendering, then you will not need Node.js on your production server, because all the JS will be executed on the client side.

If you are doing server-side prerendering (which is the default in the React-Redux template in this repo), then you do need Node.js on your production server, because the prerendering process involves executing your JavaScript code on the server.

To disable server-side prerendering do the following:

  1. Remove asp-prerender-module="ClientApp/dist/main-server" from Views>Home>Index.cshtml
  2. Remove @addtaghelper "*, Microsoft.AspNetCore.SpaServices" from Views/_ViewImports.cshtml