I'm trying to get started with Typescript for Electron development. After wrestling with getting typing for node and jquery, I finally got my .ts file error free.
The problem is now that when I run my app, I get this error:
index.js:2 Uncaught ReferenceError: exports is not defined
These are the first two lines in index.js:
"use strict"; Object.defineProperty(exports, "__esModule", { value: true });
I don't know that that line does. Typescript added it when compiling. My app works fine if I remove it.
How do I get rid of this error?
Oh and here's my tsconfig, if that's relevant.
{ "compilerOptions": { "target": "es6", "module": "commonjs", "moduleResolution": "node", "isolatedModules": false, "jsx": "react", "experimentalDecorators": true, "emitDecoratorMetadata": true, "declaration": false, "noImplicitAny": false, "noImplicitUseStrict": false, "removeComments": true, "noLib": false, "preserveConstEnums": true, "suppressImplicitAnyIndexErrors": true }, "exclude": [ "node_modules", "typings/browser", "typings/browser.d.ts" ], "compileOnSave": true, "buildOnSave": false, "atom": { "rewriteTsconfig": false } }
To solve the "ReferenceError require is not defined" error, remove the type property if it's set to module in your package. json file and rename any files that have a . mjs extension to have a . js extension.
What is CommonJS? CommonJS is a module formatting system. It is a standard for structuring and organizing JavaScript code. CJS assists in the server-side development of apps and it's format has heavily influenced NodeJS's module management.
The error "The requested module does not provide an export named 'default'" occurs when we try to import a value or a function from a file using a default import, but the value is not exported from the file with a default export.
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 .
I solved it with a hack in the embedding HTML:
<script> var exports = {}; </script> <script src="index.js"></script>
Basically giving it what it wants, a global exports variable.
With that my TypeScript (2.3.2) generated file (es6) loads.
I was also facing the same issue and tried by chagning with different versions of typescript but did not work.
Finally I got it - There was a "type": "module"
and when I removed it - it worked
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