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.

`document is not defined` related to CustomEvents

See original GitHub issue

I’m experiencing an odd issue… after a while of running my bankai server (generated by create-choo-app version ^9.0.0-1), I stopped it, and now I can’t start it back up.

First, I got an error about window not being defined in the context of window.EventSource('sse').

screen shot 2017-08-13 at 11 40 35

Recognising this as part of choo-reload, I disabled that plugin. Now I’m getting an error about document not being defined in the context of document.createEvent.

screen shot 2017-08-13 at 11 40 04

I’ve tried removing a bunch of my code, and reinstalling all dependencies, and I’m still getting the issue. Any idea why?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
louiscentercommented, Aug 27, 2017

I spent the last couple of hours trying to debug an issue which I think is related to this ticket, but I’m not using create-choo-app, or bankai@next.

I did a fresh npm install --save choo bankai. This installs choo 6.0.0 and bankai 8.1.1. I tried running the following block of code:

var choo = require('choo')
var html = require('choo/html')

var app = choo()

app.route('/', hello)
document.body.appendChild(app.start()

function hello (state, emit) {
  return html`<div>Hello world</div>`
}

bankai crashes and returns ReferenceError: document is not defined. Following the suggestion in this thread, I then ran the following block of code:

var choo = require('choo')
var html = require('choo/html')

var app = choo()

app.route('/', hello)

if (typeof window !== 'undefined') {
  document.body.appendChild(app.start())
}

function hello (state, emit) {
  return html`<div>Hello world</div>`
}

Note: the same issue with the same fix will occur if you’re using app.mount() too.

This code now works, but I’m still very confused as to why there has to be a window check even when using bankai 8.1.1 which doesn’t seem to do anything SSR related.

Is there a dependency that both bankai 8.1.1 and bankai@next are both using that has caused bankai 8.1.1 to break without a window check?

This has me concerned because if you don’t do a window check (which isn’t mentioned in either the choo README or the bankai README), your app breaks, and there’s no obvious explanation as to why. If you were a new user trying the choo/bankai stack for the first time, I feel it would be somewhat confusing.

1reaction
timwiscommented, Aug 17, 2017

Yeah I wasn’t calling that function; I just did create-choo-app. I think it’s being thrown by one of the deps?

Regarding SSR being enabled by default, is that relevant to people using bankai as a dev server, or is it an optimisation? I’ve personally not had to use SSR yet. I wouldn’t mind getting it as a free bonus, but it also means I don’t consider the implications of window being absent when I’m just throwing a basic UI together. So I wonder how we’d make that more clear, perhaps in the errors, or maybe make it so that if SSR fails the client code still runs rather than the whole app dying?

Read more comments on GitHub >

github_iconTop Results From Across the Web

ReferenceError: document is not defined · Issue #472 - GitHub
I am getting an error for custom-event library which may be use for cross browser support. Angular universal does not support DOM related...
Read more >
CustomEvent() - Web APIs | MDN
The CustomEvent() constructor creates a new CustomEvent object. ... An object that, in addition of the properties defined in Event() , can have...
Read more >
ReferenceError: document is not defined (in plain JavaScript)
When you are using server side rendering there is no browser. Hence, there will not be any variable window or document . Hence...
Read more >
Dispatching custom events - The Modern JavaScript Tutorial
We can not only assign handlers, but also generate events from ... Built-in event classes form a hierarchy, similar to DOM element classes....
Read more >
Custom events in JavaScript: A complete guide
If the custom event has cancelable set to false , calling event.preventDefault() will not perform any action.
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