How to fix the ‘Uncaught ReferenceError: exports is not defined’ error in files generated with TypeScript?

Sometimes, we want to fix the ‘Uncaught ReferenceError: exports is not defined’ error in files generated with TypeScript.

In this article, we’ll look at how to fix the ‘Uncaught ReferenceError: exports is not defined’ error in files generated with TypeScript.

How to fix the ‘Uncaught ReferenceError: exports is not defined’ error in files generated with TypeScript?

To fix the ‘Uncaught ReferenceError: exports is not defined’ error in files generated with TypeScript, we should remove the "type": "module" option from tsconfig.json.

For instance, if we have

{
  //...
  "type": "module"
  //...
}

in tsconfig.json then we should remove it so that the TypeScript compiler will transpile the code into something that doesn’t have export and import in the generated files.

Conclusiun

To fix the ‘Uncaught ReferenceError: exports is not defined’ error in files generated with TypeScript, we should remove the "type": "module" option from tsconfig.json.