Namespace node.repl
A Read-Eval-Print-Loop (REPL) is available both as a standalone program and easily
includable in other programs. REPL provides a way to interactively run
JavaScript and see the results. It can be used for debugging, testing, or
just trying things out.
By executing node without any arguments from the command-line you will be
dropped into the REPL. It has simplistic emacs line-editing.
mjr:~$ node
Type '.help' for options.
> a = [ 1, 2, 3];
[ 1, 2, 3 ]
> a.forEach(function (v) {
... console.log(v);
... });
1
2
3
For advanced line-editors, start node with the environmental variable NODENOREADLINE=1.
This will start the REPL in canonical terminal settings which will allow you to use with rlwrap.
For example, you could add this to your bashrc file:
alias node="env NODENOREADLINE=1 rlwrap node"
- Defined in: node.repl.js
Method Summary
| Method Attributes | Method Name and Description |
|---|---|
| static |
node.repl.start(prompt, source)
Starts a REPL with
prompt as the prompt and stream for all I/O. |
| static |
node.repl.writer(obj, showHidden, depth, colors)
|
Method Detail
-
static node.repl.start(prompt, source)Starts a REPL with
promptas the prompt andstreamfor all I/O.promptis optional and defaults to>.streamis optional and defaults toprocess.stdin. Multiple REPLs may be started against the same running instance of node. Each will share the same global object but will have unique I/O. Here is an example that starts a REPL on stdin, a Unix socket, and a TCP socket:var net = require("net"), repl = require("repl"); connections = 0; repl.start("node via stdin> "); net.createServer(function (socket) { connections += 1; repl.start("node via Unix socket> ", socket); }).listen("/tmp/node-repl-sock"); net.createServer(function (socket) { connections += 1; repl.start("node via TCP socket> ", socket); }).listen(5001);Running this program from the command line will start a REPL on stdin. Other REPL clients may connect through the Unix socket or TCP socket.telnetis useful for connecting to TCP sockets, andsocatcan be used to connect to both Unix and TCP sockets. By starting a REPL from a Unix socket-based server instead of stdin, you can connect to a long-running node process without restarting it.- Parameters:
- {string} prompt
- {string} source
-
static node.repl.writer(obj, showHidden, depth, colors)
- Parameters:
- {Object} obj
- {string} showHidden
- {number} depth
- {string} colors