'my namespace is not defined. Typescript
I got Visual Studio Community 2015 + Node.js Tools I have created "Blank Node.js Console Application" Typescript project
I have added one more TypeScript file, TypeScript1.ts and put following:
module MyModule {
export class Calculator {
add(x: number, y: number): number {
return x + y;
};
constructor() { }
}
}
In main file, app.ts I put this:
/// <reference path="TypeScript1.ts" />
console.log('Hello world');
var subject: MyModule.Calculator;
subject = new MyModule.Calculator();
var result: number = subject.add(2, 3);
console.log(result);
It builds without issues, though when I try to debug/run, it states:
ReferenceError: MyModule is not defined
at Object.<anonymous> (C:\Users\User\Documents\Visual Studio 2015\Projects\NodejsConsoleApp1\NodejsConsoleApp1\app.js:8:15)
What do I do wrong?
Solution 1:[1]
I would check app.js contents if it contains code from TypeScript1.ts file. My guess is that it doesn't.
You can run your tsc as follows to fix it:
tsc --module commonjs --target es5 --outFile app.js TypeScript1.ts app.ts
Please note the --outFile option.
Solution 2:[2]
I believe there are few issues (assumes TypeScript 1.5+):
First: /// <reference path="TypeScript1.ts" />
Should be: import MyModule from "./TypeScript1";
Second I believe you need to export module MyModule
Alternately you could do change
module MyModule {
export class Calculator {
add(x: number, y: number): number {
return x + y;
};
constructor() { }
}
}
To
export default class Calculator {
add(x: number, y: number): number {
return x + y;
};
constructor() { }
}
And then import Calculator from "./TypeScript1";
Or Alternately
export class Calculator {
add(x: number, y: number): number {
return x + y;
};
constructor() { }
}
And then import {Calculator} from "./TypeScript1";
Respectively depending on which of the above you chose.
The first creates the Calculator class as the primary export which is useful if the calculator is the main thing you plan to keep in the file, the second is useful if you plan to have multiple things of equal importance in the file.
Also the module keyword is generally not used as much anymore favoring instead the namespace keyword because the module keyword was too confusing and too many people confused it with file import modules.
Solution 3:[3]
It's now Christmas 2017 and they still haven't fixed this >:(. Alexanders solution is correct. For "Visual Studio Code" use the following in your tsconfig.json file:
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"module": "system",
"outFile": "bundle.js",
"target": "es5"
},
You then run your bundled file "node bundle".
Solution 4:[4]
Namespace works without even referencing... I have used something like this...
sript1.ts
namespace A{
class AA{
var x:number;
constructor(x:number){
this.x=x;
}
}
}
script2.ts
var y:A.AA = new A.AA(3);
Namespace would work even if the files are in different folders in the project. I did not reference or import any thing.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | MartyIX |
| Solution 2 | |
| Solution 3 | Darren Bailey |
| Solution 4 | Baradwaj Aryasomayajula |
