Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReferenceError: globalThis is not defined #2795

Closed
clarkdo opened this issue Jan 14, 2020 · 19 comments · Fixed by #2802
Closed

ReferenceError: globalThis is not defined #2795

clarkdo opened this issue Jan 14, 2020 · 19 comments · Fixed by #2802

Comments

@clarkdo
Copy link

clarkdo commented Jan 14, 2020

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)
@domenic
Copy link
Member

domenic commented Jan 14, 2020

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.

@clarkdo
Copy link
Author

clarkdo commented Jan 14, 2020

It looks like globalThis is supported by v8 7.0 and introduced from Node v11 .

Thanks for the quick reply 😃

@clarkdo
Copy link
Author

clarkdo commented Jan 14, 2020

BTW, I generated and compared different js-globals.json between Node v10 and current live one, the only difference is globalThis

@meetDeveloper
Copy link

Facing same bug here.

@Dangoo
Copy link

Dangoo commented May 5, 2020

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?

@domenic
Copy link
Member

domenic commented May 5, 2020

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).

@SimenB
Copy link
Contributor

SimenB commented May 6, 2020

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.

@caihaibin1991
Copy link

@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 ....)

@pawellesniowski
Copy link

@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 ....)

hi, where(what file) did you define this line :

global.globalThis = xxx

@caihaibin1991
Copy link

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

@book777
Copy link

book777 commented Oct 12, 2020

globalthis/polyfill from https://github.com/es-shims/globalThis resolve problem for me

@caihaibin1991
Copy link

my way.... global.globalThis=global; ....

@bidoubiwa
Copy link

The solution for me is to use this library https://www.npmjs.com/package/jest-environment-jsdom which creates a jest jsdom environment with the version jsdom from any jest version.

For example, by using jest-environment-jsdom version 25, you will use the jsdom from this version. And in jest 25, the jsdom version was still compatible with node 10

// package.jsom
    "jest-environment-jsdom": "25.5",
// jest config
      displayName: 'dom',
      testEnvironment: 'jest-environment-jsdom',

@sanjeev0007
Copy link

@caihaibin1991 suggestion work for me thanks!

@lesliezsy
Copy link

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

@manwindersingh1712
Copy link

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

@dep
Copy link

dep commented Apr 25, 2022

@manwindersingh1712

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

Where?

@manwindersingh1712
Copy link

@manwindersingh1712

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

Where?

I don't actually remember the use case in which I used it
What I can suggest is disable the ssr for that specific component in which you are using this package.
ref: https://nextjs.org/docs/advanced-features/dynamic-import

@igorcafe
Copy link

igorcafe commented May 6, 2022

I tried global.globalThis = global but it didn't work.
The weird thing is that I'm using node v14.19.0, and there is no other version of node installed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.