1
1
# REPL
2
2
3
- A Read-Eval-Print-Loop (REPL) is available both as a standalone program and easily
4
- includable in other programs. REPL provides a way to interactively run
5
- JavaScript and see the results. It can be used for debugging, testing, or
3
+ A Read-Eval-Print-Loop (REPL) is available both as a standalone program and
4
+ easily includable in other programs. The REPL provides a way to interactively
5
+ run JavaScript and see the results. It can be used for debugging, testing, or
6
6
just trying things out.
7
7
8
8
By executing ` node ` without any arguments from the command-line you will be
@@ -19,26 +19,39 @@ dropped into the REPL. It has simplistic emacs line-editing.
19
19
2
20
20
3
21
21
22
- For advanced line-editors, start node with the environmental variable ` NODE_NO_READLINE=1 ` .
23
- This will start the REPL in canonical terminal settings which will allow you to use with ` rlwrap ` .
22
+ For advanced line-editors, start node with the environmental variable
23
+ ` NODE_NO_READLINE=1 ` . This will start the main and debugger REPL in canonical
24
+ terminal settings which will allow you to use with ` rlwrap ` .
24
25
25
26
For example, you could add this to your bashrc file:
26
27
27
28
alias node="env NODE_NO_READLINE=1 rlwrap node"
28
29
29
30
30
- ## repl.start([ prompt ] , [ stream ] , [ eval ] , [ useGlobal ] , [ ignoreUndefined ] )
31
+ ## repl.start(options )
31
32
32
- Returns and starts a REPL with ` prompt ` as the prompt and ` stream ` for all I/O.
33
- ` prompt ` is optional and defaults to ` > ` . ` stream ` is optional and defaults to
34
- ` process.stdin ` . ` eval ` is optional too and defaults to async wrapper for
35
- ` eval() ` .
33
+ Returns and starts a ` REPLServer ` instance. Accepts an "options" Object that
34
+ takes the following values:
36
35
37
- If ` useGlobal ` is set to true, then the repl will use the global object,
38
- instead of running scripts in a separate context. Defaults to ` false ` .
36
+ - ` prompt ` - the prompt and ` stream ` for all I/O. Defaults to ` > ` .
39
37
40
- If ` ignoreUndefined ` is set to true, then the repl will not output return value
41
- of command if it's ` undefined ` . Defaults to ` false ` .
38
+ - ` input ` - the readable stream to listen to. Defaults to ` process.stdin ` .
39
+
40
+ - ` output ` - the writable stream to write readline data to. Defaults to
41
+ ` process.stdout ` .
42
+
43
+ - ` terminal ` - pass ` true ` if the ` stream ` should be treated like a TTY, and
44
+ have ANSI/VT100 escape codes written to it. Defaults to checking ` isTTY `
45
+ on the ` output ` stream upon instantiation.
46
+
47
+ - ` eval ` - function that will be used to eval each given line. Defaults to
48
+ an async wrapper for ` eval() ` . See below for an example of a custom ` eval ` .
49
+
50
+ - ` useGlobal ` - if set to ` true ` , then the repl will use the ` global ` object,
51
+ instead of running scripts in a separate context. Defaults to ` false ` .
52
+
53
+ - ` ignoreUndefined ` - if set to ` true ` , then the repl will not output the
54
+ return value of command if it's ` undefined ` . Defaults to ` false ` .
42
55
43
56
You can use your own ` eval ` function if it has following signature:
44
57
@@ -56,16 +69,32 @@ Here is an example that starts a REPL on stdin, a Unix socket, and a TCP socket:
56
69
57
70
connections = 0;
58
71
59
- repl.start("node via stdin> ");
72
+ repl.start({
73
+ prompt: "node via stdin> ",
74
+ input: process.stdin,
75
+ output: process.stdout
76
+ });
60
77
61
78
net.createServer(function (socket) {
62
79
connections += 1;
63
- repl.start("node via Unix socket> ", socket);
80
+ repl.start({
81
+ prompt: "node via Unix socket> ",
82
+ input: socket,
83
+ output: socket
84
+ }).on('exit', function() {
85
+ socket.end();
86
+ })
64
87
}).listen("/tmp/node-repl-sock");
65
88
66
89
net.createServer(function (socket) {
67
90
connections += 1;
68
- repl.start("node via TCP socket> ", socket);
91
+ repl.start({
92
+ prompt: "node via TCP socket> ",
93
+ input: socket,
94
+ output: socket
95
+ }).on('exit', function() {
96
+ socket.end();
97
+ });
69
98
}).listen(5001);
70
99
71
100
Running this program from the command line will start a REPL on stdin. Other
@@ -76,6 +105,12 @@ TCP sockets.
76
105
By starting a REPL from a Unix socket-based server instead of stdin, you can
77
106
connect to a long-running node process without restarting it.
78
107
108
+ For an example of running a "full-featured" (` terminal ` ) REPL over
109
+ a ` net.Server ` and ` net.Socket ` instance, see: https://gist.github.com/2209310
110
+
111
+ For an example of running a REPL instance over ` curl(1) ` ,
112
+ see: https://gist.github.com/2053342
113
+
79
114
### Event: 'exit'
80
115
81
116
` function () {} `
0 commit comments