Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught ReferenceError: exports is not defined in filed generated by Typescript

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     } } 
like image 750
Blargmode Avatar asked Feb 27 '17 23:02

Blargmode


People also ask

How do you fix uncaught ReferenceError require is not defined?

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?

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.

Does not provide an export named default?

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.

Is not defined in ES module scope?

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 .


2 Answers

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.

like image 144
Markus Hahn Avatar answered Oct 02 '22 16:10

Markus Hahn


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

like image 35
Vikas Chauhan Avatar answered Oct 02 '22 14:10

Vikas Chauhan