I am learning typeScript with visual studio and trying to do a simple class export. I have seen this problem many times, but none of the solutions helped me. what am I doing wrong ?
- I have changed module system from CommonJs to system
- I have installed npm systemJs
- tried instead of "import" to write "/// ...reference path.... / "
still the same error "Uncaught ReferenceError: exports is not defined at..."
import { Address } from "./address";
class Customer {
protected name: string = "";
public addressObj: Address = new Address();
private _CustomerName: string = "";
public set CustomerName(value: string) {
if (value.length == 0) {
throw "Customer name is requaierd"
}
this._CustomerName = value;
}
public get CustomerName(): string {
return this._CustomerName;
}
}
export class Address {
public street1: string = "";
}
<!doctype html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
</head>
<body>
<script src="address.js"></script>
<script src="Customer.js"></script>
<script>
try {
cust = new Customer();
cust.CustomerName = "doron";
cust.addressObj.street1 = "test"
} catch (ex) {
alert(ex);
}
</script>
</body>
</html>
what else am I not doing ?!?!
CommonJs isn't needed. Wrap all typescript in a common namespace and then export other namespaces, class, etc. Does offer intellisense:
Instantiate SubClass2
I've just solved this. Or rather I found a blog post that does https://blorkfish.wordpress.com/2012/10/23/typescript-organizing-your-code-with-amd-modules-and-require-js/. To follow the proper SO guidelines I will reproduce it here.
Why you get 'exports is undefined' is due to Visual Studio transpiling to use
commonjs
modules. I looked at this for days trying different things and the message seemed to be that commonjs is the default and should "just work" (TM). But doesn't. I don't know what is missing - perhaps VS needs some include. I couldn't work out what.The transpiled class using
commonjs
will include lines like this at the top and bottom of the .js file:This is your error.
The solution that worked for me was to use
requirejs
. It is an implementation of AMD (http://requirejs.org/docs/whyamd.html). It still usesexports
but wraps it in adefine
:What follows is pretty-much the blog post https://blorkfish.wordpress.com/2012/10/23/typescript-organizing-your-code-with-amd-modules-and-require-js/. With some modifications for the most recent version of Typescript. Unfortunately where I'm working I don't have access to github (or anything like that) so I can only paste my files in here.
I also had to allow for some issues with Visual Studio. I found that despite configuring the
project.csproj
's<TypescriptModuleKind>
to beAMD
, it seemed to always default tocommonjs
. So I transpile manually and hope to find a solution to stop VS defaulting.I created a
tsconfig.json
file (tsc --init
) and set themodule
toamd
. I added a"Files": ["*.ts"]
.Commented out lines (with defaults) removed.
After starting the server, Visual Studio would transpile the files (using
commonjs
module format). Requiring me to runtsc
to force the files to transpile to use theamd
module format. And it worked (no errors seen in the developer console).First the files layout (from blogpost). What I have is the same except i've called my file
index.html
asdefault.htm
reminds me of the bad ole days. You can getrequire.js
from http://requirejs.org/. Andrequire.d.ts
from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/requirejs (save theindex.d.ts
asrequire.d.ts
).Greeter.ts
AppConfig.ts
AppMain.ts
Or use:
And just realise that what is called an 'error' when you transpile is just a warning!:
app.ts
No longer used
index.html