jsdom: ReferenceError: globalThis is not defined

Basic info:

  • Node.js version: v10.16.1 (actually all v10 versions)
  • jsdom version: v16

Minimal reproduction case

const { JSDOM } = require("jsdom");

const options = {
  runScripts: "dangerously"
};
const dom = new JSDOM(``, options);

Then you’ll see the error, since Node.js doesn’t support globalThis but js-globals.json includes globalThis and runInContext('globalThis', windowInstance) is executed in https://github.com/jsdom/jsdom/blob/master/lib/jsdom/browser/Window.js#L48

ReferenceError: globalThis is not defined
    at evalmachine.<anonymous>:1:1
    at Script.runInContext (vm.js:133:20)
    at Object.runInContext (vm.js:311:6)
    at setupWindow (/Users/clark/projects/nuxt.js/node_modules/jsdom/lib/jsdom/browser/Window.js:49:55)
    at new Window (/Users/clark/projects/nuxt.js/node_modules/jsdom/lib/jsdom/browser/Window.js:105:3)
    at new JSDOM (/Users/clark/projects/nuxt.js/node_modules/jsdom/lib/api.js:36:20)
    at repl:1:1
    at Script.runInThisContext (vm.js:122:20)
    at REPLServer.defaultEval (repl.js:332:29)
    at bound (domain.js:402:14)

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 16
  • Comments: 19 (3 by maintainers)

Commits related to this issue

Most upvoted comments

Hi @domenic , still getting this error. Setup:

  • node: v10.20 (applies to 10x)
  • jsdom: v16.2.2
  • jest: v26.0.0
evalmachine.<anonymous>:1
    globalThis
    ^

    ReferenceError: globalThis is not defined

      at evalmachine.<anonymous>:1:1
      at setupWindow (../../node_modules/jsdom/lib/jsdom/browser/Window.js:51:55)
      at new Window (../../node_modules/jsdom/lib/jsdom/browser/Window.js:107:3)
      at exports.createWindow (../../node_modules/jsdom/lib/jsdom/browser/Window.js:38:10)
      at new JSDOM (../../node_modules/jsdom/lib/api.js:36:20)

Any ideas?

my way… global.globalThis=global; …

FWIW, this reproduces the issue:

// polyfill globalThis for older nodes
globalThis = global;
const { JSDOM } = require('jsdom');
new JSDOM('', {runScripts: 'dangerously'});

Setting globalThis to anything at all will reproduce, I just picked global to be somewhat realistic. An even more realistic polyfill might be require('core-js'); which will also break, and might (I haven’t verified) be inserted by @babel/preset-env and/or @babel/runtime.

Not sure it’s necessarily something that needs to be fixed/changed in JSDOM, but it might be made more robust*? Anyways, hopes this helps folks hitting this issue to track it down. It’s not Jest itself that does this.

*) e.g. filtering on vm.runInContext('this', windowInstance) rather than global in the outer context. This might not be feasible, I haven’t played with it.

Thanks for finding this! This will probably work in Node.js v13, but we mean to support v10, so this is a bug. We’ll try to get it fixed ASAP.

I updated the node version from v11.15.0 to LTS (v15.11.0, npm v7.6.0), and then it works.

@clarkdo global.globalThis = xxx I use this way to fix this problem on node v.10.x. my enviroment is node v.10.x and there are too much application , I can not upgrade the version of node…(I am afraid to do …)

@manwindersingh1712

You can use global.globalThis, this worked for me 🚀

Where?

@pawellesniowski you can include a js file, and define this line in the js file.

You’ll need to post a new issue similar to the one in the OP, including the minimal reproduction case (not involving other technology like Jest).