TypeError: Cannot read property '_location' of null
See original GitHub issueBasic 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:
- Created 6 years ago
- Reactions:2
- Comments:12 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
For me, this arose from updating
jest-environment-jsdomfrom 27.0.6 to 27.2.2, which both rely onjsdom@^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 onwindow.closewith 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 stablejsdomversion.Yup, looks like anything that has to do with node’s I/O will trigger this, here’s another example:
main.js
script.js
Results in: