Javascriptservices: Exception: Call to Node module failed with error: ReferenceError: window is not defined when importing angular2/platform/browser

Created on 30 May 2016  路  3Comments  路  Source: aspnet/JavaScriptServices

I'm trying to import {Title} from angular2/platform/browser in order to change page's title, but node is throwing a exception. I tryed to pass Title as a directive or provider, with no success. Coult someone help?

Exception: Call to Node module failed with error: ReferenceError: window is not defined
at Object. (C:\JavaScriptServices\samples\angular\MusicStore\node_modules\angular2\src\facade\browser.js:5:11)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:456:32)
at tryModuleLoad (module.js:415:12)
at Function.Module._load (module.js:407:3)
at Module.require (module.js:466:17)
at require (internal/module.js:20:19)
at Object. (C:\JavaScriptServices\samples\angular\MusicStore\node_modules\angular2\srcplatform\browser\tools\common_tools.js:4:17)
at Module._compile (module.js:541:32)
Microsoft.AspNetCore.NodeServices.HttpNodeInstance.d__5`1.MoveNext() in HttpNodeInstance.cs, line 47

My code is:

import *  as Browser from 'angular2/platform/browser';
@ng.Component({
   (...)
    directives: [router.ROUTER_DIRECTIVES, Browser.Title],
}) 

export class Produto {
constructor(private title: Browser.Title) { (...) }
}

Stack:

Exception: Call to Node module failed with error: ReferenceError: window is not defined at Object. (C:\JavaScriptServices\samples\angular\MusicStore\node_modules\angular2\src\facade\browser.js:5:11) at Module._compile (module.js:541:32) at Object.Module._extensions..js (module.js:550:10) at Module.load (module.js:456:32) at tryModuleLoad (module.js:415:12) at Function.Module._load (module.js:407:3) at Module.require (module.js:466:17) at require (internal/module.js:20:19) at Object. (C:\JavaScriptServices\samples\angular\MusicStore\node_modules\angular2\srcplatform\browser\tools\common_tools.js:4:17) at Module._compile (module.js:541:32)

Raw:
System.Exception: Call to Node module failed with error: ReferenceError: window is not defined
at Object. (C:\JavaScriptServices\samples\angular\MusicStore\node_modules\angular2\src\facade\browser.js:5:11)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:456:32)
at tryModuleLoad (module.js:415:12)
at Function.Module._load (module.js:407:3)
at Module.require (module.js:466:17)
at require (internal/module.js:20:19)
at Object. (C:\JavaScriptServices\samples\angular\MusicStore\node_modules\angular2\srcplatform\browser\tools\common_tools.js:4:17)
at Module._compile (module.js:541:32)
at Microsoft.AspNetCore.NodeServices.HttpNodeInstance.d__51.MoveNext() in C:\JavaScriptServices\src\Microsoft.AspNetCore.NodeServices\HostingModels\HttpNodeInstance.cs:line 47 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()
at Microsoft.AspNetCore.NodeServices.OutOfProcessNodeInstance.d__121.MoveNext() in C:\JavaScriptServices\src\Microsoft.AspNetCore.NodeServices\HostingModels\OutOfProcessNodeInstance.cs:line 43 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()
at Microsoft.AspNetCore.SpaServices.Prerendering.Prerenderer.d__2.MoveNext() in C:\JavaScriptServices\src\Microsoft.AspNetCore.SpaServices\Prerendering\Prerenderer.cs:line 20
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.AspNetCore.SpaServices.Prerendering.PrerenderTagHelper.d__23.MoveNext() in C:\JavaScriptServices\src\Microsoft.AspNetCore.SpaServices\Prerendering\PrerenderTagHelper.cs:line 60
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner.d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Asp._Views_Home_Index_cshtml.d__33.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Razor.RazorView.d__14.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Razor.RazorView.d__13.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.ViewResult.d__26.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.d__44.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.d__43.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.d__42.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.d__37.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.d__32.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Internal.MvcRouteHandler.d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Builder.RouterMiddleware.d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.d__7.MoveNext()

Most helpful comment

@SteveSandersonMS may be we should have some clear API for such render (whole document from js, without razor)? It could be really helpfull

For example

public IActionResult Spa()
{
  return _prerenderer.Run("module/path.ts", "config.js", args);
}

All 3 comments

Hi, you try to prerender stuff which is highly coupled with window object. This is not possible during limitiation of Node runtime (there is now window object when your code run in node env). You should use some ng-universal or create your own abstractions araound window and DOM stuff to be able to share your code between client and server.

Take a look on _https://github.com/angular/universal/blob/master/modules/universal/src/node/platform/dom/node_dom_renderer.ts_ this file. It contain node specific implementation for DomRendere which contain Title.

Like @laskoviymishka mentioned, there is no window or document title on the server. If you absolutely need to generate a title dynamically in your Angular 2 app on the server, you might need your angular root component's template to output the entire HTML page including a <title> element, instead of injecting an angular component into a Razor page.

@SteveSandersonMS may be we should have some clear API for such render (whole document from js, without razor)? It could be really helpfull

For example

public IActionResult Spa()
{
  return _prerenderer.Run("module/path.ts", "config.js", args);
}
Was this page helpful?
0 / 5 - 0 ratings