Aspnetcore-angular-universal: Exception: Call to Node module failed with error: ReferenceError: window is not defined

Created on 3 Feb 2017  路  3Comments  路  Source: TrilonIO/aspnetcore-angular-universal

first request works fine on on refresh throws error

Exception: Call to Node module failed with error: ReferenceError: window is not defined
at Ng2Webstorage.initStorageListener (/Users/roman_petrenko/git/source/elixir/node_modules/ng2-webstorage/bundles/core.umd.js:354:13)
at new Ng2Webstorage (/Users/roman_petrenko/git/source/elixir/node_modules/ng2-webstorage/bundles/core.umd.js:332:14)
at NgModuleInjector.AppServerModuleInjector.createInternal (/AppServerModule/module.ngfactory.js:576:28)
at NgModuleInjector.create (/Users/roman_petrenko/git/source/elixir/node_modules/@angular/core/bundles/core.umd.js:8979:80)
at NgModuleFactory.create (/Users/roman_petrenko/git/source/elixir/node_modules/@angular/core/bundles/core.umd.js:8953:22)
at /Users/roman_petrenko/git/source/elixir/node_modules/@angular/core/bundles/core.umd.js:8523:65
at ZoneDelegate.invoke (/Users/roman_petrenko/git/source/elixir/node_modules/zone.js/dist/zone-node.js:229:26)
at Object.onInvoke (/Users/roman_petrenko/git/source/elixir/node_modules/@angular/core/bundles/core.umd.js:4427:41)
at ZoneDelegate.invoke (/Users/roman_petrenko/git/source/elixir/node_modules/zone.js/dist/zone-node.js:228:32)
at Zone.run (/Users/roman_petrenko/git/source/elixir/node_modules/zone.js/dist/zone-node.js:113:43)
at NgZone.run (/Users/roman_petrenko/git/source/elixir/node_modules/@angular/core/bundles/core.umd.js:4296:66)
at PlatformRef_._bootstrapModuleFactoryWithZone (/Users/roman_petrenko/git/source/elixir/node_modules/@angular/core/bundles/core.umd.js:8521:27)
at PlatformRef_.bootstrapModuleFactory (/Users/roman_petrenko/git/source/elixir/node_modules/@angular/core/bundles/core.umd.js:8504:25)
at NodePlatform.bootstrapModule (/Users/roman_petrenko/git/modules/platform-node/node-platform.ts:505:31)
at NodePlatform.serializeModule (/Users/roman_petrenko/git/modules/platform-node/node-platform.ts:153:12)

FAQ

Most helpful comment

Thanks a lot for advice!!!!!!!!!!! You rocks!

All 3 comments

So it's a problem with that library you're using Ng2Webstorage. I believe that library wasn't built with webworkers/universal in mind, they're just accessing the window without testing to see if it's undefined first. Maybe you can create an issue with that repo so they can try and fix those issues?

When you refresh, it's rendering that part of the application on the server first, so that's why the exception Exception: Call to Node module failed with error: ReferenceError: window is not defined gets fired :(

On the bright side, there's actually a nice StorageService built into the project, that handles localStorage in a platform-specific way you can use :)

It uses Dependency injection, so on the server it's just getting/setting a local variable (you could connect this to something like Redis/etc if you'd want to), and on the browser it's using localStorage

You can see an example of it in use on the platform-examples page here

constructor (private storage: StorageService) {}

ngOnInit() {
  this.storage.setItem('test', 'This came from Storage within each Platform !!');
  let storedItem = this.storage.getItem('test');
}

Hope that helps!

Thanks a lot for advice!!!!!!!!!!! You rocks!

Hey @MarkPieszak , was this service removed? I ran into an issue by using localstorage directly, then the server times out since localstorage does not exist on the server.

I know how to implement the service on the client and add it to the browser module declaration, however I'm not sure how to implement the server side functionality.

Any advice?
Thank you

Was this page helpful?
0 / 5 - 0 ratings

Related issues

odahcam picture odahcam  路  4Comments

rcanpahali picture rcanpahali  路  3Comments

peterdobson picture peterdobson  路  4Comments

bugproof picture bugproof  路  5Comments

michael-vasyliv picture michael-vasyliv  路  4Comments