Menztrual
Menztrual

Reputation: 41597

Custom REPL in nodejs

I'm trying to play with the nodejs built in REPL from the documentation.

http://nodejs.org/api/repl.html

The example of adding an item is as follows:

repl.start().context.m = msg;

I cant seem to find away to add multiple menus. I've tried doing:

menus = {m = 'hello', f = 'foo'}
repl.start().context = menus

But that doesn't work either. I get:

testREPL> m
TypeError: needs a 'context' argument.
    at REPLServer.self.eval (repl.js:113:21)
    at Interface.<anonymous> (repl.js:250:12)
    at Interface.EventEmitter.emit (events.js:88:17)
    at Interface._onLine (readline.js:199:10)
    at Interface._normalWrite._line_buffer (readline.js:308:12)
    at Array.forEach (native)
    at Interface._normalWrite (readline.js:307:11)
    at Socket.ondata (readline.js:90:10)
    at Socket.EventEmitter.emit (events.js:115:20)
    at TCP.onread (net.js:395:14)

Does anybody know how to get this working?

Upvotes: 2

Views: 1613

Answers (1)

Linus Thiel
Linus Thiel

Reputation: 39223

You can't assign to the context property, you have to add properties to it. What you are trying is "overwriting" it with your own object. Try to assign each property by itself instead:

var context = repl.start({}).context;
context.m = 'hello';
context.f = 'foo';

Upvotes: 4

Related Questions