Uncaught ReferenceError: export is not defined in file generated by Typescript

I am trying to get started with Typescript to develop Electron. After struggling with getting text for node and jquery, I finally got a free .ts file.

The problem is that when I run my application, 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 do not know what this line is. Typescript added it when compiling. My application works fine if I remove it.

How to get rid of this error?

Oh, and here is my tsconfig, if 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 } } 
+8
javascript typescript
source share
5 answers

There is a problem with the new version of typescript 2.2.1, try using the older version 2.1.6, which solved the same problem that you have for me.

Version 2.2.1 at compilation adds this line Object.defineProperty(exports, "__esModule", { value: true }); , while the previous version 2.1.6 does not.

+7
source share

I solved this with a hack in embedding HTML:

 <script> var exports = {}; </script> <script src="index.js"></script> 

Basically providing him with what he wants, a global export variable.

The TypeScript file (2.3.2) (es6) is loaded with this.

+26
source share

Quickfix

Change "target": "es6" to "target": "es5" in tsconfig.json.

0
source share

I had the same issue with the js file generated by the Typescript compiler. Same line:

 Object.defineProperty(exports, "__esModule", { value: true }); 

And the same error:

 game.js:2 Uncaught ReferenceError: exports is not defined 

I defined the Game class in this file. I solved the problem by adding this to the end of my game.ts file:

 export = Game; 

With this replaced Typescript compiler:

 Object.defineProperty(exports, "__esModule", { value: true }); 

from:

 module.exports = Game; 

There will be no more mistakes for me after that.

0
source share

I had the same problem, I just modified the systemjs.config.js file as below

'npm:': '/ node_modules /' - // Its value was just 'node_modules /' and I added '/' at the beginning

'app': '/ src / app' - // Its value was just an โ€œapplicationโ€, and the path to the folder of my application was different, so it changed it accordingly.

loader: '/ src / systemjs- angular-loader.js' - // Its value was just โ€œsystemjs- angular-loader.jsโ€, and since its location was different in my project, it therefore pointed it to the correct path

0
source share

All Articles