Articles
typescript
How to fix ReferenceError: globalThis is not defined typescript

"ReferenceError: globalThis is not defined" is an error produced by some newer JavaScript libraries as they access globalThis.

How to fix

To fix this error you need to be using a version of node12+ however this may not solve the error on some browsers.

Install the polyfill

I recommend adding a polyfill for globalThis to ensure that the error is fixed over every environment.

npm i globalthis

inside polyfill.js add the following line

import 'globalthis/polyfill';

or import it into your file using

var globalThis = require('globalthis/polyfill')();