Search code examples
node.jstypescriptwebstorm

TypeScript / NodeJS: variable not defined, using internal ref paths for separate files


I have been testing TypeScript with node, all was going well until i tried splitting these out.

Am I forced to use modules ?

I have 2 files, app.ts which has a reference path to the hellofile.tst

/// <reference path="hellofile.ts" />
var testme = new Hello()
console.log(testme.testMe());

and the hellofile.ts which contains

class Hello {
    testMe():string {
        return "hello";
    }
}

Now running the program (i am using webstorm), I get the following error.

/usr/local/bin/node app.js

/

Users/tst/WebstormProjects/NodeJsWithTypescript/app.js:2
var testme = new Hello();
                 ^
ReferenceError: Hello is not defined
    at Object.<anonymous> (/Users/tst/WebstormProjects/NodeJsWithTypescript/app.js:2:18)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:906:3

Process finished with exit code 8

Solution

  • You don't have to use modules. You can compile with --out flag

    But external modules with commonjs is highly recommended if targeting nodejs : http://m.youtube.com/watch?v=KDrWLMUY0R0