1

I'm getting this error:

Uncaught ReferenceError: exports is not defined

It seems like this question has been asked and answered several times. This is an example of this question being asked.

The only problem I'm having is that I can't find the tsconfig.ts file people keep mentioning in their answers. There's no such file in VS solution.

I'm using Visual Studio 2017 Enterprise Edition.

EDIT

I define all the common types in a file called CommonTypes.ts

class ddlData {
    ctrId: string;
    id: string;
}

interface ISelectedIdsObj {
    facilityId: number;
    AllCk: boolean;
    AllSelected: boolean;
    Ids: number[];
    totalItems: number;
}

//More classes and intefaces here...

export { ddlData, ISelectedIdsObj, IPaginationData, IHeaderColumn, ISelectedListItem }

This is how I call it

/// <reference path="../typings/jquery/jquery.d.ts" />

//import { ddlData, ISelectedIdsObj, IPaginationData, IHeaderColumn, ISelectedListItem } from '../Commons/CommonTypes'

I'm getting the Uncaught ReferenceError: exports is not defined at line 3.

EDIT 2

I've created 2 typescript files: test1 and test2.

This is test2:

export const x = 1;

and this is test1:

/// <reference path="../typings/jquery/jquery.d.ts" />

import { x } from './Test2'

$(function () {
   alert(x);
});

I'm still getting the same error: Uncaught ReferenceError: exports is not defined at Test1.js:3

Richard77
  • 20,343
  • 46
  • 150
  • 252
  • Who ever is trying to close the question and down voted me because it's not clear. I'm trying to import a series of classes using ` import { class1, class2, ...} from '../path'`. I'm getting the error I posted above. I've done some researches and found some answers. But I don't know whether those solutions applied to Visual Studio. If you don't understand my question why not explain why you find difficult instead of wanting to close the question? – Richard77 Jul 08 '17 at 02:03
  • Can you post your code? – GregHNZ Jul 08 '17 at 02:07
  • Please see edit. – Richard77 Jul 08 '17 at 02:15
  • Do you have `tsconfig.*json*` in your solution? If yes, can you add the contents to question? – Pratik Gaikwad Jul 08 '17 at 02:20
  • There's no such file in the solution. That's why I asked the question. Because it seems like tsconfig.*json* can be found in VS Code not in VS. – Richard77 Jul 08 '17 at 02:22
  • You can add it by using *Add new item* but it doesn't matter. Can you try removing this line `export { ddlData, ISelectedIdsObj, IPaginationData, IHeaderColumn, ISelectedListItem }` and adding `export` to each individual entity as mentioned in https://www.typescriptlang.org/docs/handbook/modules.html? – Pratik Gaikwad Jul 08 '17 at 02:30
  • No luck. I'm still getting the same error. – Richard77 Jul 08 '17 at 02:35
  • It is likely there are some other errors in your files. Try to do the simplest `export const x = 1` in one file and `import { x } from './thatfile'` in another and see what you get – unional Jul 08 '17 at 02:54
  • instead of `//More classes and intefaces here...` can you put actual code? If you are still getting error after declaring each entity separately as `export` that means there is some error in the rest of the code or some other file. – Pratik Gaikwad Jul 08 '17 at 03:04
  • @PratikGaikwad, those classes and interfaces where in the same file as the one containing all the functions. And it was working fine. All I did was copy/paste them to another file then comment out in the first file. – Richard77 Jul 08 '17 at 15:56
  • @unional, please see the second edit.Now that I'm researching on the error, I've started to suspect that it is related with some javascript loader: something like `Requirejs` and `CommonJs`. – Richard77 Jul 08 '17 at 16:24
  • @Richard77 sorry for delay. Just quick question, are you getting this error in browser? Is this error occurring in client side code or server side code? – Pratik Gaikwad Jul 08 '17 at 22:52
  • by the way, tsconfig.json is just a file that you create at the root of your project. – unional Jul 09 '17 at 05:48
  • @unional, I'm getting the error in the browser. In visual, everything is just fine. I'm getting even the intellisense. It's when I run the application, nothing is happening. When I press F12, I see the error. – Richard77 Jul 09 '17 at 06:48
  • Can you post your compiled `Test1.js`? – unional Jul 09 '17 at 07:01

1 Answers1

0

Since you have clarified that the issue is occurring in browser, it is easy to fix. As you don't have created tsconfig.json file in your solution, your project is compiled with default configuration in VS. i.e. it will use CommonJS for as target module. More info about tsconfig.json can be read here.

This is the actual reason behind the error. Web browsers don't support CommonJS by default. The same issue will occur with require(...) also. You will need to use either module loaders such as RequireJS or amd or you can use packaging tools such as webpack or browserify which does this for you. Webpack is easy to configure and diverse packaging tool that has gained lot of popularity.

See SO answer

Pratik Gaikwad
  • 1,526
  • 2
  • 21
  • 44