Typescript ReferenceError: exports is not defined

George C.

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"
    ]
}
iFreilicht

EDIT:

This answer might not work depending if you're not targeting es5 anymore, I'll try to make the answer more complete.

Original Answer

If CommonJS isn't installed (which defines exports), you have to remove this line from your tsconfig.json:

 "module": "commonjs",

As per the comments, this alone may not work with later versions of tsc. If that is the case, you can install a module loader like CommonJS, SystemJS or RequireJS and then specify that.

Note:

Look at your main.js file that tsc generated. You will find this at the very top:

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

It is the root of the error message, and after removing "module": "commonjs",, it will vanish.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Uncaught ReferenceError: exports is not defined and require

From Dev

Typescript and NodeJS : ReferenceError : Module is not defined

From Dev

Uncaught ReferenceError: define is not defined typescript

From Dev

Typescript Uncaught ReferenceError: notifyOwner is not defined

From Dev

Error when migrating from Babel 5 to 6 (ReferenceError: exports is not defined)

From Dev

ReferenceError: user is not defined at Function.module.exports.getUserById passportjs

From Dev

Typescript Angularjs Uncaught ReferenceError: app is not defined

From Dev

typescript-uncaught referenceError: require is not defined

From Dev

ReferenceError: is not defined

From Dev

ReferenceError: $ is not defined

From Dev

ReferenceError: $ is not defined

From Dev

ReferenceError: $ is not defined or Uncaught ReferenceError: $ is not defined

From Dev

Declare var in typescript from external file gets ReferenceError: ... is not defined

From Dev

Angular with TypeScript: ReferenceError: System is not defined System.config

From Dev

Can TypeScript exports be overloaded?

From Dev

Uncaught ReferenceError: [functionName] is not defined

From Dev

Uncaught ReferenceError: WebKitPoint is not defined

From Dev

Uncaught ReferenceError: getPrice is not defined

From Dev

Uncaught ReferenceError: Collection is not defined

From Dev

ReferenceError: printStackTrace is not defined

From Dev

Uncaught ReferenceError: 'functionName' not defined

From Dev

"ReferenceError: this is not defined" in subclass

From Dev

ReferenceError: MouseEvent is not defined

From Dev

ReferenceError: Require is not defined (Webstorm)

From Dev

Uncaught ReferenceError: i is not defined

From Java

ReferenceError: describe is not defined NodeJs

From Dev

Uncaught ReferenceError: grecaptcha is not defined

From Dev

ReferenceError: angular is not defined in WebStorm

From Dev

Uncaught ReferenceError: $$ is not defined

Related Related

  1. 1

    Uncaught ReferenceError: exports is not defined and require

  2. 2

    Typescript and NodeJS : ReferenceError : Module is not defined

  3. 3

    Uncaught ReferenceError: define is not defined typescript

  4. 4

    Typescript Uncaught ReferenceError: notifyOwner is not defined

  5. 5

    Error when migrating from Babel 5 to 6 (ReferenceError: exports is not defined)

  6. 6

    ReferenceError: user is not defined at Function.module.exports.getUserById passportjs

  7. 7

    Typescript Angularjs Uncaught ReferenceError: app is not defined

  8. 8

    typescript-uncaught referenceError: require is not defined

  9. 9

    ReferenceError: is not defined

  10. 10

    ReferenceError: $ is not defined

  11. 11

    ReferenceError: $ is not defined

  12. 12

    ReferenceError: $ is not defined or Uncaught ReferenceError: $ is not defined

  13. 13

    Declare var in typescript from external file gets ReferenceError: ... is not defined

  14. 14

    Angular with TypeScript: ReferenceError: System is not defined System.config

  15. 15

    Can TypeScript exports be overloaded?

  16. 16

    Uncaught ReferenceError: [functionName] is not defined

  17. 17

    Uncaught ReferenceError: WebKitPoint is not defined

  18. 18

    Uncaught ReferenceError: getPrice is not defined

  19. 19

    Uncaught ReferenceError: Collection is not defined

  20. 20

    ReferenceError: printStackTrace is not defined

  21. 21

    Uncaught ReferenceError: 'functionName' not defined

  22. 22

    "ReferenceError: this is not defined" in subclass

  23. 23

    ReferenceError: MouseEvent is not defined

  24. 24

    ReferenceError: Require is not defined (Webstorm)

  25. 25

    Uncaught ReferenceError: i is not defined

  26. 26

    ReferenceError: describe is not defined NodeJs

  27. 27

    Uncaught ReferenceError: grecaptcha is not defined

  28. 28

    ReferenceError: angular is not defined in WebStorm

  29. 29

    Uncaught ReferenceError: $$ is not defined

HotTag

Archive