{ "source": "doc/api/repl.markdown", "modules": [ { "textRaw": "REPL", "name": "repl", "desc": "

A Read-Eval-Print-Loop (REPL) is available both as a standalone program and\neasily includable in other programs. The REPL provides a way to interactively\nrun JavaScript and see the results. It can be used for debugging, testing, or\njust trying things out.\n\n

\n

By executing node without any arguments from the command-line you will be\ndropped into the REPL. It has simplistic emacs line-editing.\n\n

\n
mjr:~$ node\nType '.help' for options.\n> a = [ 1, 2, 3];\n[ 1, 2, 3 ]\n> a.forEach(function (v) {\n...   console.log(v);\n...   });\n1\n2\n3
\n

For advanced line-editors, start node with the environmental variable\nNODE_NO_READLINE=1. This will start the main and debugger REPL in canonical\nterminal settings which will allow you to use with rlwrap.\n\n

\n

For example, you could add this to your bashrc file:\n\n

\n
alias node="env NODE_NO_READLINE=1 rlwrap node"
\n", "methods": [ { "textRaw": "repl.start(options)", "type": "method", "name": "start", "desc": "

Returns and starts a REPLServer instance. Accepts an "options" Object that\ntakes the following values:\n\n

\n\n

You can use your own eval function if it has following signature:\n\n

\n
function eval(cmd, context, filename, callback) {\n  callback(null, result);\n}
\n

Multiple REPLs may be started against the same running instance of node. Each\nwill share the same global object but will have unique I/O.\n\n

\n

Here is an example that starts a REPL on stdin, a Unix socket, and a TCP socket:\n\n

\n
var net = require("net"),\n    repl = require("repl");\n\nconnections = 0;\n\nrepl.start({\n  prompt: "node via stdin> ",\n  input: process.stdin,\n  output: process.stdout\n});\n\nnet.createServer(function (socket) {\n  connections += 1;\n  repl.start({\n    prompt: "node via Unix socket> ",\n    input: socket,\n    output: socket\n  }).on('exit', function() {\n    socket.end();\n  })\n}).listen("/tmp/node-repl-sock");\n\nnet.createServer(function (socket) {\n  connections += 1;\n  repl.start({\n    prompt: "node via TCP socket> ",\n    input: socket,\n    output: socket\n  }).on('exit', function() {\n    socket.end();\n  });\n}).listen(5001);
\n

Running this program from the command line will start a REPL on stdin. Other\nREPL clients may connect through the Unix socket or TCP socket. telnet is useful\nfor connecting to TCP sockets, and socat can be used to connect to both Unix and\nTCP sockets.\n\n

\n

By starting a REPL from a Unix socket-based server instead of stdin, you can\nconnect to a long-running node process without restarting it.\n\n

\n

For an example of running a "full-featured" (terminal) REPL over\na net.Server and net.Socket instance, see: https://gist.github.com/2209310\n\n

\n

For an example of running a REPL instance over curl(1),\nsee: https://gist.github.com/2053342\n\n

\n", "events": [ { "textRaw": "Event: 'exit'", "type": "event", "name": "exit", "desc": "

function () {}\n\n

\n

Emitted when the user exits the REPL in any of the defined ways. Namely, typing\n.exit at the repl, pressing Ctrl+C twice to signal SIGINT, or pressing Ctrl+D\nto signal "end" on the input stream.\n\n

\n

Example of listening for exit:\n\n

\n
r.on('exit', function () {\n  console.log('Got "exit" event from repl!');\n  process.exit();\n});
\n", "params": [] } ], "signatures": [ { "params": [ { "name": "options" } ] } ] } ], "miscs": [ { "textRaw": "REPL Features", "name": "REPL Features", "type": "misc", "desc": "

Inside the REPL, Control+D will exit. Multi-line expressions can be input.\nTab completion is supported for both global and local variables.\n\n

\n

The special variable _ (underscore) contains the result of the last expression.\n\n

\n
> [ "a", "b", "c" ]\n[ 'a', 'b', 'c' ]\n> _.length\n3\n> _ += 1\n4
\n

The REPL provides access to any variables in the global scope. You can expose\na variable to the REPL explicitly by assigning it to the context object\nassociated with each REPLServer. For example:\n\n

\n
// repl_test.js\nvar repl = require("repl"),\n    msg = "message";\n\nrepl.start().context.m = msg;
\n

Things in the context object appear as local within the REPL:\n\n

\n
mjr:~$ node repl_test.js\n> m\n'message'
\n

There are a few special REPL commands:\n\n

\n\n

The following key combinations in the REPL have these special effects:\n\n

\n\n" } ], "type": "module", "displayName": "REPL" } ] }