Prerendering failed because of error: ReferenceErr

2019-09-20 07:37发布

问题:

I am new in Angular2, I am trying to go through functionalities with a demo project and used prerendering with Webpack in the code.

My code is here-

https://github.com/abrarjahin/Dot.NetCore_Angular2_App

I am getting this error-

An unhandled exception occurred while processing the request.

Exception: Call to Node module failed with error: Prerendering failed because of error: ReferenceError: window is not defined
at D:\Dot.NetCore_Angular2_App\ProfileManagement\ClientApp\dist\main-server.js:1:6541
at D:\Dot.NetCore_Angular2_App\ProfileManagement\ClientApp\dist\main-server.js:1:6473
at e.exports (D:\Dot.NetCore_Angular2_App\ProfileManagement\ClientApp\dist\main-server.js:1:6759)
at Object.<anonymous> (D:\Dot.NetCore_Angular2_App\ProfileManagement\ClientApp\dist\main-server.js:1:3513)
at t (D:\Dot.NetCore_Angular2_App\ProfileManagement\ClientApp\dist\main-server.js:1:217)
at Object.<anonymous> (D:\Dot.NetCore_Angular2_App\ProfileManagement\ClientApp\dist\main-server.js:1:3381)
at t (D:\Dot.NetCore_Angular2_App\ProfileManagement\ClientApp\dist\main-server.js:1:217)
at Object.e.exports (D:\Dot.NetCore_Angular2_App\ProfileManagement\ClientApp\dist\main-server.js:1:2992)
at t (D:\Dot.NetCore_Angular2_App\ProfileManagement\ClientApp\dist\main-server.js:1:217)
at Object.<anonymous> (D:\Dot.NetCore_Angular2_App\ProfileManagement\ClientApp\dist\main-server.js:1:1615)
at t (D:\Dot.NetCore_Angular2_App\ProfileManagement\ClientApp\dist\main-server.js:1:217)
at Object.<anonymous> (D:\Dot.NetCore_Angular2_App\ProfileManagement\ClientApp\dist\main-server.js:1:779)
at t (D:\Dot.NetCore_Angular2_App\ProfileManagement\ClientApp\dist\main-server.js:1:217)
at Zone.current.fork.name (D:\Dot.NetCore_Angular2_App\ProfileManagement\ClientApp\dist\main-server.js:1:310)
at Object.<anonymous> (D:\Dot.NetCore_Angular2_App\ProfileManagement\ClientApp\dist\main-server.js:1:315)
Current directory is: D:\Dot.NetCore_Angular2_App\ProfileManagement 

I never used any window in my code.

Does anyone have any idea why I am getting this error?


Re-

I had checked this question - AngularJS ReferenceError: $window is not defined

But it is not helping.


Thanks in advance for helping.

回答1:

I had the same issue when import npm modules, I fixed this by replacing prerender attribute for app component at Index.cshtml page:

<app asp-prerender-module="ClientApp/dist/main-server">Loading...</app>

to

<app asp-ng2-prerender-module="ClientApp/dist/main-server">Loading...</app>

Maybe it helps you.