1

I have put in 17 hours trying to get this to work. I have tried just about every tutorial I could find.

I have an existing Angular 4 project that can be build and run perfectly without Universal. The existing project can even run through AOT fine (which was another pain to get running).

The current error I encounter when trying to run the server-side rendering is:

ERROR { ReferenceError: document is not defined 

It took a while to even get to this point. There were tons of @types/node errors, but now that I got it to compile, the page won't load.

After doing some research on the error, it may be possible that ngx-bootstrap has something to do with it, but I am not 100% sure.

I am using this tutorial in combination with the webpack version of universal because without webpack my project would not compile due to ngx-bootstrap.

3
  • When following the tutorial, which step specifically could you not complete? At the moment this question is very broad. Commented Jul 29, 2017 at 19:48
  • You are adding universal to a Angular 4 project? Why? Commented Jul 29, 2017 at 19:51
  • The first tutorial would not work unless I implemented the steps shown in the second tutorial. When I run npm run start from the second tutorial, I get that error after the bundle compiles Commented Jul 29, 2017 at 20:36

1 Answer 1

3

Somewhere in your code (or in a library), there is a reference to the document object. While this object is defined in every browser, it is not available on the server.

According to https://github.com/angular/universal#universal-gotchas

window, document, navigator, and other browser types - do not exist on the server - so using them, or any library that uses them (jQuery for example) will not work.

As far as I can tell, there is no easy solution to this issue other than rewriting the code to avoid those objects.

Sign up to request clarification or add additional context in comments.

1 Comment

This was it! The problem was in the index.html, there was a js script referencing the document. Additionally, I needed to install systemjs. If anyone else ever has this problem, install systemjs, and do a folder search for the term "document" and remove them each until the problem is gone. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.