javascript - How to use Typescript class in Nodejs REPL? - Stack Overflow

I created a foo.ts like this:class Foo{public echo(){console.log("foo");}}And it outputs jav

I created a foo.ts like this:

class Foo{
    public echo(){
    console.log("foo");
    }
}

And it outputs javascript code like this:

var Foo = (function () {
    function Foo() {
    }
    Foo.prototype.echo = function () {
        console.log("foo");
    };
    return Foo;
})();

I want to call echo function in nodejs REPL, but it ends up a error like this:

$ node
> require('./foo.js');
{}
> f = new Foo
ReferenceError: Foo is not defined
    at repl:1:10
    at REPLServer.self.eval (repl.js:110:21)
    at Interface.<anonymous> (repl.js:239:12)
    at Interface.EventEmitter.emit (events.js:95:17)
    at Interface._onLine (readline.js:202:10)
    at Interface._line (readline.js:531:8)
    at Interface._ttyWrite (readline.js:760:14)
    at ReadStream.onkeypress (readline.js:99:10)
    at ReadStream.EventEmitter.emit (events.js:98:17)
    at emitKey (readline.js:1095:12)

How can I instantiate the class and call the function echo?

I created a foo.ts like this:

class Foo{
    public echo(){
    console.log("foo");
    }
}

And it outputs javascript code like this:

var Foo = (function () {
    function Foo() {
    }
    Foo.prototype.echo = function () {
        console.log("foo");
    };
    return Foo;
})();

I want to call echo function in nodejs REPL, but it ends up a error like this:

$ node
> require('./foo.js');
{}
> f = new Foo
ReferenceError: Foo is not defined
    at repl:1:10
    at REPLServer.self.eval (repl.js:110:21)
    at Interface.<anonymous> (repl.js:239:12)
    at Interface.EventEmitter.emit (events.js:95:17)
    at Interface._onLine (readline.js:202:10)
    at Interface._line (readline.js:531:8)
    at Interface._ttyWrite (readline.js:760:14)
    at ReadStream.onkeypress (readline.js:99:10)
    at ReadStream.EventEmitter.emit (events.js:98:17)
    at emitKey (readline.js:1095:12)

How can I instantiate the class and call the function echo?

Share Improve this question asked Jun 14, 2014 at 8:05 ironsandironsand 15.3k18 gold badges89 silver badges188 bronze badges 1
  • 2 I'm not sure how typescript works, but it's clear that you are not exporting anything from Foo.js (and you are not assign the required module to anything). Maybe make yourself familiar either Node's module system first. – Felix Kling Commented Jun 14, 2014 at 8:09
Add a ment  | 

2 Answers 2

Reset to default 4

Node.js does not have a leaky global like the browser window object.

To use TypeScript code in node.js you need to use monjs and export the class i.e.

class Foo{
    public echo(){
    console.log("foo");
    }
}

export = Foo;

Then in the REPL:

$ node
> var Foo = require('./foo.js');
{}
> f = new Foo();

To learn more about AMD / CommonJS : https://www.youtube./watch?v=KDrWLMUY0R0&hd=1

Update for modern times: You can use ES modules easily thanks to esm:

export class Foo{
    public echo(){
        console.log("foo");
    }
}

In the REPL:

$ node -r esm
> import { Foo } from './foo.js';
{}
> f = new Foo();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745349686a4623755.html

相关推荐

  • javascript - How to use Typescript class in Nodejs REPL? - Stack Overflow

    I created a foo.ts like this:class Foo{public echo(){console.log("foo");}}And it outputs jav

    10小时前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信