Typescript ReferenceError: exports is not defined

George C. picture George C. · Mar 27, 2017 · Viewed 201.4k times · Source

Trying to implement a module following the official handbook, I get this error message:

Uncaught ReferenceError: exports is not defined

at app.js:2

But nowhere in my code do I ever use the name exports.

How can I fix this?


Files

app.ts

let a = 2;
let b:number = 3;

import Person = require ('./mods/module-1');

module-1.t

 export class Person {
  constructor(){
    console.log('Person Class');
  }
}
export default Person;

tsconfig.json

{
   "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "noImplicitAny": false,
        "sourceMap": true,
        "outDir": "scripts/"
    },
    "exclude": [
        "node_modules"
    ]
}

Answer

Venkatesh Muniyandi picture Venkatesh Muniyandi · Oct 23, 2017

Few other Solutions for this issue

  • Add the following line before other references to Javascript. This is a nice little hack.
   <script>var exports = {};</script>
  • This issue occurs with the latest version of TypeScript, this error can be eliminated by referring to typescript version 2.1.6