question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ReferenceError: document is not defined

See original GitHub issue
  • I’m submitting a …
- [x] bug report
- [ ] feature request
- [ ] support request => Please do not submit support request here, see note at the top of this template.
  • What modules are related to this Issue?
- [ ] aspnetcore-engine
- [x] express-engine
- [ ] hapi-engine
  • Do you want to request a feature or report a bug? Bug.

  • What is the current behavior? When you try and enter your server bundle on localhost:4000 in the browser you get an error saying ReferenceError: document is not defined for one of my services which handles cookies.

  • If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem by creating a github repo.

Follow the story here, run the bundle and go to localhost:4000.

https://github.com/angular/angular-cli/wiki/stories-universal-rendering

  • What is the expected behavior? I expect the bundle to run and/or fail silently for this kind of error, or if there’s some kind of polyfill I can use.

  • What is the motivation / use case for changing the behavior? If I have a service which uses document how can I avoid this error for being thrown? It’s not like I can just exclude it from the server bundle because basically every module uses this service.

  • Please tell us about your environment:

  • Angular version: 4.4.6
  • Browser: [all]
  • Language: [all]
  • OS: [all]
  • Platform: [NodeJs]

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8

github_iconTop GitHub Comments

20reactions
skreborncommented, Dec 6, 2017

Please read the Universal “Gotchas” section of the README. It explains both why document isn’t available and how to check for the current platform to write conditional code.

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.

import { PLATFORM_ID } from '@angular/core';
import { isPlatformBrowser, isPlatformServer } from '@angular/common';

constructor(@Inject(PLATFORM_ID) private platformId: Object) { ... }

ngOnInit() {
  if (isPlatformBrowser(this.platformId)) {
     // Client only code.
     ...
  }
  if (isPlatformServer(this.platformId)) {
    // Server only code.
    ...
  }
}
2reactions
chriseugenerodriguezcommented, Jul 31, 2018
// SSR
import { isPlatformBrowser } from '@angular/common';
import { Injectable, Inject, PLATFORM_ID } from '@angular/core';

constructor(@Inject(PLATFORM_ID) private platformId: Object) {
   // BROWSER
   if (isPlatformBrowser(this.platformId)) {

   }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fix ReferenceError: Document is not defined ... - Sabe.io
The most common reason for getting the reference error while on the browser is when you try to access the document object too...
Read more >
ReferenceError: document is not defined in JavaScript
The "ReferenceError: document is not defined" error occurs for multiple reasons: ... The document relates to the document object which represents a web...
Read more >
ReferenceError: document is not defined (in plain JavaScript)
Explanation: The error is caused because NextJs renders the page in the server only and in the server document (document is used inside...
Read more >
How To Fix ReferenceError document is not defined ... - Isotropic
If you are trying to use the document object and receiving a ReferenceError: document is not defined error then there is a good...
Read more >
How to solve the document is not defined error - Flavio Copes
Here's how to fix the “referenceerror: document is not defined” error that you might have in Node.js or with a tool like Next.js....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found