To solve the "Uncaught ReferenceError: exports is not defined", add a script tag that defines an exports variable, e.g. <script>var exports = {};</script> above your JS script tag if in the browser, or remove the type attribute if set to module in your package.
The error "Module is not defined in ES module scope" occurs when we try to use the module. exports CommonJS syntax in ES modules. To solve the error, use the export keyword to export a member from a file, e.g. export const num = 42 .
Getting Started. From a structure perspective, a CommonJS module is a reusable piece of JavaScript that exports specific objects made available to any dependent code. Unlike AMD, there are typically no function wrappers around such modules (so we won't see define here, for example).
Few other Solutions for this issue
<script>var exports = {};</script>
EDIT:
This answer might not work depending if you're not targeting es5
anymore, I'll try to make the answer more complete.
Original Answer
If CommonJS isn't installed (which defines exports
), you have to remove this line from your tsconfig.json
:
"module": "commonjs",
As per the comments, this alone may not work with later versions of tsc
. If that is the case, you can install a module loader like CommonJS, SystemJS or RequireJS and then specify that.
Note:
Look at your main.js
file that tsc
generated. You will find this at the very top:
Object.defineProperty(exports, "__esModule", { value: true });
It is the root of the error message, and after removing "module": "commonjs",
, it will vanish.
Remove "type": "module"
from package.json
.
npm install @babel/plugin-transform-modules-commonjs
and add to to .babelrc plugins resolved my question.
This is fixed by setting the module
compiler option to es6
:
{
"compilerOptions": {
"module": "es6",
"target": "es5",
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With