Search code examples
javascripttypescripttsconfig

Typescript setup in VisualStudio 2015 giving error: `exports is not defined`. How to correctly setup?


I just started with typescript, I am going through export import in typescript

but when I started I am getting an error on exports,

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

and the error is

Uncaught ReferenceError: exports is not defined

after some google I go through one solution

but as per that answer my project should contain tsconfig.json, common.js

but I don't have any of these

here is screenshot of my project

enter image description here

what should I do?


Solution

  • module.exports, or the shorter version exports is an object that only exists in Node.js

    Trying to use the variable in the browser will give you an Uncaught reference error: exports is not defined.

    Node.js documentation reference for the exports shortcut

    You should add a tsconfig.json to your project, in which you set module to es6 for example.

    Your settings probably default to "commonjs", which expects you to have an exports object, which you do not have in the browser. Take a look here for some additional reading.