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.
8
8
By executing `node` without any arguments from the command-line you will be
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`.
25
26
For example, you could add this to your bashrc file:
27
28
alias node="env NODE_NO_READLINE=1 rlwrap node"
30
## repl.start([prompt], [stream], [eval], [useGlobal], [ignoreUndefined])
32
Starts a REPL with `prompt` as the prompt and `stream` for all I/O. `prompt`
33
is optional and defaults to `> `. `stream` is optional and defaults to
34
`process.stdin`. `eval` is optional too and defaults to async wrapper for
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`.
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`.
31
## repl.start(options)
33
Returns and starts a `REPLServer` instance. Accepts an "options" Object that
34
takes the following values:
36
- `prompt` - the prompt and `stream` for all I/O. Defaults to `> `.
38
- `input` - the readable stream to listen to. Defaults to `process.stdin`.
40
- `output` - the writable stream to write readline data to. Defaults to
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.
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`.
50
- `useColors` - a boolean which specifies whether or not the `writer` function
51
should output colors. If a different `writer` function is set then this does
52
nothing. Defaults to the repl's `terminal` value.
54
- `useGlobal` - if set to `true`, then the repl will use the `global` object,
55
instead of running scripts in a separate context. Defaults to `false`.
57
- `ignoreUndefined` - if set to `true`, then the repl will not output the
58
return value of command if it's `undefined`. Defaults to `false`.
60
- `writer` - the function to invoke for each command that gets evaluated which
61
returns the formatting (including coloring) to display. Defaults to
43
64
You can use your own `eval` function if it has following signature:
45
function eval(cmd, callback) {
66
function eval(cmd, context, filename, callback) {
46
67
callback(null, result);
59
repl.start("node via stdin> ");
81
prompt: "node via stdin> ",
83
output: process.stdout
61
86
net.createServer(function (socket) {
63
repl.start("node via Unix socket> ", socket);
89
prompt: "node via Unix socket> ",
92
}).on('exit', function() {
64
95
}).listen("/tmp/node-repl-sock");
66
97
net.createServer(function (socket) {
68
repl.start("node via TCP socket> ", socket);
100
prompt: "node via TCP socket> ",
103
}).on('exit', function() {
71
108
Running this program from the command line will start a REPL on stdin. Other
76
113
By starting a REPL from a Unix socket-based server instead of stdin, you can
77
114
connect to a long-running node process without restarting it.
116
For an example of running a "full-featured" (`terminal`) REPL over
117
a `net.Server` and `net.Socket` instance, see: https://gist.github.com/2209310
119
For an example of running a REPL instance over `curl(1)`,
120
see: https://gist.github.com/2053342
126
Emitted when the user exits the REPL in any of the defined ways. Namely, typing
127
`.exit` at the repl, pressing Ctrl+C twice to signal SIGINT, or pressing Ctrl+D
128
to signal "end" on the `input` stream.
130
Example of listening for `exit`:
132
r.on('exit', function () {
133
console.log('Got "exit" event from repl!');