TypeError: Cannot read property '_location' of null

See original GitHub issue

Basic info:

  • Node.js version: v8.9.1
  • jsdom version: 11.6.0

Minimal reproduction case

When there is a network request, if the window is closed after the message is sent but before the message is received, the code will continue to run and any window access will result in this kind of error:

(node:15703) TypeError: Cannot read property '_location' of null
    at Window.get location [as location] (/Users/Stefan/Desktop/Work/html-render/node_modules/jsdom/lib/jsdom/browser/Window.js:188:79)
    at fetch.then.then (./script.js:2:10)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)

In this example I polyfill fetch and make a request from an external script. As soon as the window loads I close it.

main.js

const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const window = (new JSDOM(`<html>
    <head></head>
    <body>
        <script src="./script.js"></script>
    </body>
</html>`, {
  runScripts: "dangerously",
  resources: 'usable',
})).window;

window.fetch = require('node-fetch');

window.onload = () => {
  window.close();
};

script.js

fetch('https://api.ipify.org').then(x => x.text()).then((ip) => {
  window.location.href = ip;
});

How does similar code behave in browsers?

Window closes and any network request is aborted.

Since fetch in this case is an external implementation and uses node’s I/O callback, i’m not sure how that callback can be stopped.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
merrywhethercommented, Sep 27, 2021

For me, this arose from updating jest-environment-jsdom from 27.0.6 to 27.2.2, which both rely on jsdom@^16.6.0.

The issue in this case was a call to self.close() after a promise resolved inside of some WebWorker code that was under test (and previously working). Replacing the spy on window.close with a full mock fixed the issue, seemingly pointing to the promise escaping the test lifetime. This thread pointed me towards window methods and async, but it seemed like Jest might be the root cause for my instance of this given the stable jsdom version.

3reactions
legraphistacommented, Jan 22, 2018

Yup, looks like anything that has to do with node’s I/O will trigger this, here’s another example:

main.js

const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const { spawn } = require('child_process');

const window = (new JSDOM(`<html>
    <head></head>
    <body>
        <script src="./script.js"></script>
    </body>
</html>`, {
  runScripts: "dangerously",
  resources: 'usable',
})).window;

window.sleep = (cb) => {
  const p = spawn('sleep', [1]);
  p.on('close', cb);
};

window.onload = () => {
  setTimeout(() => window.close(), 100);
};

script.js

sleep(() => window.location.href);

Results in:

Stefans-MacBook:html-render Stefan$ node --trace-warnings main.js 
/Users/Stefan/Desktop/Work/html-render/node_modules/jsdom/lib/jsdom/browser/Window.js:188
      return idlUtils.wrapperForImpl(idlUtils.implForWrapper(window._document)._location);
                                                                              ^

TypeError: Cannot read property '_location' of null
    at Window.get location [as location] (/Users/Stefan/Desktop/Work/html-render/node_modules/jsdom/lib/jsdom/browser/Window.js:188:79)
    at ChildProcess.sleep (./script.js:1:20)
    at emitTwo (events.js:126:13)
    at ChildProcess.emit (events.js:214:7)
    at maybeClose (internal/child_process.js:925:16)
    at Socket.stream.socket.on (internal/child_process.js:346:11)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at Pipe._handle.close [as _onclose] (net.js:554:12)

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I fix this issue TypeError: Cannot read property ...
How do I fix this issue TypeError: Cannot read property 'location' of undefined? ; import { AppBar, Toolbar ; MenuItem, Menu, Typography ;...
Read more >
"Cannot read property 'location' of null" error in javascript #244
Hi. I've recently integrated javascript error reporting and started to see some exceptions from the intercom javascript script.
Read more >
Uncaught TypeError: Cannot read property of null - iDiallo
This error occurs when you read a property or call a method on a null object . That's because the DOM API returns...
Read more >
cannot read properties of undefined (reading 'location ...
This happens when the browser tries to access the history object. You can fix this by adding a history object to routing. The...
Read more >
React-router Cannot read property 'location' of undefined
Uncaught TypeError: Cannot read property 'querySelector' of null. You get this error when you try to access a member (method/property) of an object...
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