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

Solution 1:

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.

Solution 2:

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

Solution 3:

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

Version 2.2.1 on compiling adds this line Object.defineProperty(exports, "__esModule", { value: true }); while the older 2.1.6 does not.

Solution 4:

I've fixed mine with the following:

tsconfig.json

{
    "compilerOptions": {
        "target": "ESNext",
        "module": "CommonJS",
        "lib": [
            "DOM",
            "ES5"
        ],
        "esModuleInterop": true
    }
}

Adding the esModuleInterop

Removing the "type": "module" from the package.json