{ "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 easily\nincludable in other programs. REPL provides a way to interactively run\nJavaScript and see the results. It can be used for debugging, testing, or\njust trying things out.\n\n
\nBy executing node
without any arguments from the command-line you will be\ndropped into the REPL. It has simplistic emacs line-editing.\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
\nFor advanced line-editors, start node with the environmental variable NODE_NO_READLINE=1
.\nThis will start the REPL in canonical terminal settings which will allow you to use with rlwrap
.\n\n
For example, you could add this to your bashrc file:\n\n
\nalias node="env NODE_NO_READLINE=1 rlwrap node"
\n",
"methods": [
{
"textRaw": "repl.start([prompt], [stream], [eval], [useGlobal], [ignoreUndefined])",
"type": "method",
"name": "start",
"desc": "Starts a REPL with prompt
as the prompt and stream
for all I/O. prompt
\nis optional and defaults to >
. stream
is optional and defaults to\nprocess.stdin
. eval
is optional too and defaults to async wrapper for\neval()
.\n\n
If useGlobal
is set to true, then the repl will use the global object,\ninstead of running scripts in a separate context. Defaults to false
.\n\n
If ignoreUndefined
is set to true, then the repl will not output return value\nof command if it's undefined
. Defaults to false
.\n\n
You can use your own eval
function if it has following signature:\n\n
function eval(cmd, callback) {\n callback(null, result);\n}
\nMultiple 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
\nHere is an example that starts a REPL on stdin, a Unix socket, and a TCP socket:\n\n
\nvar net = require("net"),\n repl = require("repl");\n\nconnections = 0;\n\nrepl.start("node via stdin> ");\n\nnet.createServer(function (socket) {\n connections += 1;\n repl.start("node via Unix socket> ", socket);\n}).listen("/tmp/node-repl-sock");\n\nnet.createServer(function (socket) {\n connections += 1;\n repl.start("node via TCP socket> ", socket);\n}).listen(5001);
\nRunning 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
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
\n", "signatures": [ { "params": [ { "name": "prompt", "optional": true }, { "name": "stream", "optional": true }, { "name": "eval", "optional": true }, { "name": "useGlobal", "optional": true }, { "name": "ignoreUndefined", "optional": true } ] } ] } ], "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
\nThe special variable _
(underscore) contains the result of the last expression.\n\n
> [ "a", "b", "c" ]\n[ 'a', 'b', 'c' ]\n> _.length\n3\n> _ += 1\n4
\nThe 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
// repl_test.js\nvar repl = require("repl"),\n msg = "message";\n\nrepl.start().context.m = msg;
\nThings in the context
object appear as local within the REPL:\n\n
mjr:~$ node repl_test.js\n> m\n'message'
\nThere are a few special REPL commands:\n\n
\n.break
- While inputting a multi-line expression, sometimes you get lost\nor just don't care about completing it. .break
will start over..clear
- Resets the context
object to an empty object and clears any\nmulti-line expression..exit
- Close the I/O stream, which will cause the REPL to exit..help
- Show this list of special commands..save
- Save the current REPL session to a file\n\n.save ./file/to/save.js
\n
.load
- Load a file into the current REPL session.\n\n.load ./file/to/load.js
\n
The following key combinations in the REPL have these special effects:\n\n
\n<ctrl>C
- Similar to the .break
keyword. Terminates the current\ncommand. Press twice on a blank line to forcibly exit.<ctrl>D
- Similar to the .exit
keyword.