Prerendering error when published to local IIS #868

Closed
opened 2025-08-09 17:17:59 +00:00 by fergalmoran · 0 comments
Owner

Originally created by @RaviDesai on 3/30/2017

I've had mostly terrific success using the templates as a guide to develop an SPA using DotNetCore, Angular2, Webpack, HMR, Server Pre-rendering, I've even wedged in Authorization using OpenIDConnect. I can run locally in IIS Express (F5 from Visual Studio) and everything works swimmingly. I can build and run in Debug or Release, and it all works as expected. However, when I publish to my Local IIS instance on the same machine (as a Release build) I get the following error in the event viewer:

Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware
An unhandled exception has occurred: Call to Node module failed with error: Prerendering failed because of error: Error: Cannot find module 'angular2-universal-polyfills'
    at Function.Module._resolveFilename (module.js:470:15)
    at Function.Module._load (module.js:418:25)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (C:\inetpub\wwwroot\galileo_permissions\ClientApp\dist\main-server.js:88:19)
    at __webpack_require__ (C:\inetpub\wwwroot\galileo_permissions\ClientApp\dist\main-server.js:20:30)
    at Object.<anonymous> (C:\inetpub\wwwroot\galileo_permissions\ClientApp\dist\main-server.js:49:2)
    at __webpack_require__ (C:\inetpub\wwwroot\galileo_permissions\ClientApp\dist\main-server.js:20:30)
    at Object.defineProperty.value (C:\inetpub\wwwroot\galileo_permissions\ClientApp\dist\main-server.js:40:18)
    at Object.<anonymous> (C:\inetpub\wwwroot\galileo_permissions\ClientApp\dist\main-server.js:43:10)
Current directory is: C:\inetpub\wwwroot\galileo_permissions

The above error seems to indicate that the webpack vendor configuration isn't been seen or used by the pre-rendering node server. But I'm not sure why it would work in IIS Express and not IIS. I've looked into the node version on my machine (7.7.4), and which is installed at C:\Program Files\NodeJS which is in my system Path, so IIS should be picking that version up. I've tried copying the webpacked vendor files into different directories, but that didn't seem to help either.

The directory structure on my IIS server is as follows:

Under C:\InetPub\wwwroot\galileo_permissions

+----ClientApp\
                tsconfig.json
                dist\
                        main-server.js
                        main.js
+----refs\
                (a load of .dll files)
+----Views\
                Account\   (contains razor files for Login and Logout, Forbidden, etc.)
                Home\   (contains Index.cshtml)
                Shared\   (contains _Layout.cshtml and Error page)
                _ViewImports.cshtml
                _ViewStart.cshtml
+----wwwroot\
                 dist\
                        main-client.js
                        vendor.js
                        vendor.css
                        (other webpack --config webpack.config.vendor.js files)
+----appsettings.json
+----(other miscellaneous stuff - not relevant here)

The section of my Razor template that references the asp-prerender-module is here (I believe it to be relatively straightfoward).

<app asp-prerender-module="ClientApp/dist/main-server"
     asp-prerender-data= "new { Security = new { 
        AuthorizedUsers = @ViewBag.LoggedInUsers,
        User = @galileoUser,
        BearerToken = @bearerToken,
        Config = @OptionsApplicationConfiguration.Value
     } }">Loading...</app>
<script src="~/dist/vendor.js" asp-append-version="true"></script>
@section scripts {
    <script src="~/dist/main-client.js" asp-append-version="true"></script>
}

The ~/dist/main-client.js and ~/dist/vendor.js are deployed under the site's wwwroot directory. They are built by the two webpack steps (webpack --env.prod and webpack --config webpack.config.vendor --env.prod) which are added into my .csproj file as a PrepublishScript target:

<Target Name="PrepublishScript" BeforeTargets="PrepareForPublish">
    <Exec Command="yarn install" />
    <Exec Command="node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.js --env.prod" />
    <Exec Command="node node_modules/webpack/bin/webpack.js --env.prod" />
  </Target>

Because the template references the server module from "ClientApp/dist/main-server" I did need to add the following to my .pubxml (just before the </Project> tag, so the main-server could be located by IIS:

  <Target Name="CustomAfterPublish" AfterTargets="Publish">
    <Message Text="********************************** CustomAfterPublish ***********************************" Importance="high"/>
    <Exec Command="powershell.exe -ExecutionPolicy Bypass -noprofile if (!(Test-Path -path '$(PublishDir)ClientApp\dist')) {New-Item '$(PublishDir)ClientApp\dist' -Type Directory}" />
    <Exec Command="powershell.exe -ExecutionPolicy Bypass -noprofile Copy-Item '$(MSBuildThisFileDirectory)..\..\ClientApp\dist\main*.js' -Destination '$(PublishDir)ClientApp\dist'" />
  </Target>

Any information you can given me about publishing to IIS so that the pre-rendering works be greatly appreciated.

*Originally created by @RaviDesai on 3/30/2017* I've had mostly terrific success using the templates as a guide to develop an SPA using DotNetCore, Angular2, Webpack, HMR, Server Pre-rendering, I've even wedged in Authorization using OpenIDConnect. I can run locally in IIS Express (F5 from Visual Studio) and everything works swimmingly. I can build and run in Debug or Release, and it all works as expected. However, when I publish to my Local IIS instance on the same machine (as a Release build) I get the following error in the event viewer: ``` Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware An unhandled exception has occurred: Call to Node module failed with error: Prerendering failed because of error: Error: Cannot find module 'angular2-universal-polyfills' at Function.Module._resolveFilename (module.js:470:15) at Function.Module._load (module.js:418:25) at Module.require (module.js:498:17) at require (internal/module.js:20:19) at Object.<anonymous> (C:\inetpub\wwwroot\galileo_permissions\ClientApp\dist\main-server.js:88:19) at __webpack_require__ (C:\inetpub\wwwroot\galileo_permissions\ClientApp\dist\main-server.js:20:30) at Object.<anonymous> (C:\inetpub\wwwroot\galileo_permissions\ClientApp\dist\main-server.js:49:2) at __webpack_require__ (C:\inetpub\wwwroot\galileo_permissions\ClientApp\dist\main-server.js:20:30) at Object.defineProperty.value (C:\inetpub\wwwroot\galileo_permissions\ClientApp\dist\main-server.js:40:18) at Object.<anonymous> (C:\inetpub\wwwroot\galileo_permissions\ClientApp\dist\main-server.js:43:10) Current directory is: C:\inetpub\wwwroot\galileo_permissions ``` The above error seems to indicate that the webpack vendor configuration isn't been seen or used by the pre-rendering node server. But I'm not sure why it would work in IIS Express and not IIS. I've looked into the node version on my machine (7.7.4), and which is installed at `C:\Program Files\NodeJS` which is in my system Path, so IIS should be picking that version up. I've tried copying the webpacked vendor files into different directories, but that didn't seem to help either. The directory structure on my IIS server is as follows: Under C:\InetPub\wwwroot\galileo_permissions ``` +----ClientApp\ tsconfig.json dist\ main-server.js main.js +----refs\ (a load of .dll files) +----Views\ Account\ (contains razor files for Login and Logout, Forbidden, etc.) Home\ (contains Index.cshtml) Shared\ (contains _Layout.cshtml and Error page) _ViewImports.cshtml _ViewStart.cshtml +----wwwroot\ dist\ main-client.js vendor.js vendor.css (other webpack --config webpack.config.vendor.js files) +----appsettings.json +----(other miscellaneous stuff - not relevant here) ``` The section of my Razor template that references the asp-prerender-module is here (I believe it to be relatively straightfoward). ``` <app asp-prerender-module="ClientApp/dist/main-server" asp-prerender-data= "new { Security = new { AuthorizedUsers = @ViewBag.LoggedInUsers, User = @galileoUser, BearerToken = @bearerToken, Config = @OptionsApplicationConfiguration.Value } }">Loading...</app> <script src="~/dist/vendor.js" asp-append-version="true"></script> @section scripts { <script src="~/dist/main-client.js" asp-append-version="true"></script> } ``` The `~/dist/main-client.js` and `~/dist/vendor.js` are deployed under the site's `wwwroot` directory. They are built by the two webpack steps (`webpack --env.prod` and `webpack --config webpack.config.vendor --env.prod`) which are added into my .csproj file as a PrepublishScript target: ``` <Target Name="PrepublishScript" BeforeTargets="PrepareForPublish"> <Exec Command="yarn install" /> <Exec Command="node node_modules/webpack/bin/webpack.js --config webpack.config.vendor.js --env.prod" /> <Exec Command="node node_modules/webpack/bin/webpack.js --env.prod" /> </Target> ``` Because the template references the server module from "ClientApp/dist/main-server" I did need to add the following to my .pubxml (just before the `</Project>` tag, so the main-server could be located by IIS: ``` <Target Name="CustomAfterPublish" AfterTargets="Publish"> <Message Text="********************************** CustomAfterPublish ***********************************" Importance="high"/> <Exec Command="powershell.exe -ExecutionPolicy Bypass -noprofile if (!(Test-Path -path '$(PublishDir)ClientApp\dist')) {New-Item '$(PublishDir)ClientApp\dist' -Type Directory}" /> <Exec Command="powershell.exe -ExecutionPolicy Bypass -noprofile Copy-Item '$(MSBuildThisFileDirectory)..\..\ClientApp\dist\main*.js' -Destination '$(PublishDir)ClientApp\dist'" /> </Target> ``` Any information you can given me about publishing to IIS so that the pre-rendering works be greatly appreciated.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github/JavaScriptServices#868
No description provided.