简体   繁体   中英

Typescript and NodeJS : ReferenceError : Module is not defined

I am making a NodeJs console application with NodeJS Tools for Visual Studio ( http://nodejstools.codeplex.com/ ) with the Typescript template.

Here is basicaly my code :

app.ts :

/// <reference path="Module/Module.ts" />
var foo = new Module.ModuleClass();
foo.foo();

Module/Module.ts :

module Module {
    export class ModuleClass {
       foo() {
        console.log('Hello World');
       }
   }
}

The compiler run without trouble but on runtime, NodeJS can't find the module. Here is the error :

var foo = new Module.ModuleClass();
              ^
ReferenceError : Module is not defined

I tired a lot of thing about that problem (using or not the /// but I can't find any solution.

Excuse me for my English, I am not a native speaker. Thanks in advance !

You need to export Module in Module/Module.ts

export module Module { ...

In app.ts, you need to require('./Module/Module')

import M = require('./Module/Module');
var foo = new M.Module.ModuleClass();
foo.foo();

You need to do this because this node.js uses the commonjs module system, which typescript supports via its "external" modules feature and a compiler arg --module commonjs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM