0

I have updated my application from angular version 5 to latest. Here's detail:

  Angular CLI: 6.2.1
  Node: 8.12.0
  OS: win32 x64
  Angular: 6.1.6

Also I am using this: ng-toolkit-universal to achieve server side rendering. I am able to build application using command "npm run build:prod" , but facing following issue while running command: "npm run server":

enter image description here

Can anyone please help or suggest some soution for the same.

Thanks in advance!

11
  • 1
    Have you used the window Object in your Angular App? If you have, It's advisable to use Renderer2 for doing DOM Manipulations instead of directly using the window or the document object. Commented Sep 18, 2018 at 17:32
  • Have you looked at the answer in this post OR this post? Commented Sep 18, 2018 at 18:18
  • Hi @SiddAjmera, Thanks for reply! Yes, I am using window object but not using Renderer2. Let me try that. Commented Sep 19, 2018 at 5:28
  • That explains why you're getting this error. Angular Universal doesn't have access to the window or the document object as it runs on Server. That's why Angular Team recommends use of Renderer2 Commented Sep 19, 2018 at 5:29
  • @SiddAjmera Actually, i am new to this and trying my hands on it. I hope Renderer2 will resolve my problem. Do u know any good example/link for the same in angular 6? Commented Sep 19, 2018 at 5:59

1 Answer 1

7

Assuming you are done SSR with angular universal. Put following code in your server.ts after imports. Build and run.

const domino = require("domino");
const fs = require("fs");
const path = require("path");
const templateA = fs
  .readFileSync(path.join("dist/browser", "index.html"))
  .toString();
const win = domino.createWindow(templateA);
win.Object = Object;
win.Math = Math;

global["window"] = win;
global["document"] = win.document;
global["branch"] = null;
global["object"] = win.object;
global['HTMLElement'] = win.HTMLElement;
global['navigator'] = win.navigator;
global['localStorage'] = localStorage;
Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately this didn't work for me. I am using angular 14(same version of angular universal as well)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.