{ "source": "doc/api/repl.md", "modules": [ { "textRaw": "REPL", "name": "repl", "stability": 2, "stabilityText": "Stable", "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

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

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

For advanced line-editors, start Node.js 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

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

\n
alias node="env NODE_NO_READLINE=1 rlwrap node"\n
", "modules": [ { "textRaw": "Environment Variable Options", "name": "environment_variable_options", "desc": "

The built-in repl (invoked by running node or node -i) may be controlled\nvia the following environment variables:

\n\n", "type": "module", "displayName": "Environment Variable Options" }, { "textRaw": "Persistent History", "name": "persistent_history", "desc": "

By default, the REPL will persist history between node REPL sessions by saving\nto a .node_repl_history file in the user's home directory. This can be\ndisabled by setting the environment variable NODE_REPL_HISTORY="".

\n", "modules": [ { "textRaw": "NODE_REPL_HISTORY_FILE", "name": "node_repl_history_file", "stability": 0, "stabilityText": "Deprecated: Use `NODE_REPL_HISTORY` instead.", "desc": "

Previously in Node.js/io.js v2.x, REPL history was controlled by using a\nNODE_REPL_HISTORY_FILE environment variable, and the history was saved in JSON\nformat. This variable has now been deprecated, and your REPL history will\nautomatically be converted to using plain text. The new file will be saved to\neither your home directory, or a directory defined by the NODE_REPL_HISTORY\nvariable, as documented here.

\n", "type": "module", "displayName": "NODE_REPL_HISTORY_FILE" } ], "type": "module", "displayName": "Persistent History" } ], "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

Core modules will be loaded on-demand into the environment. For example,\naccessing fs will require() the fs module as global.fs.

\n

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

\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
// repl_test.js\nconst repl = require('repl');\nvar msg = 'message';\n\nrepl.start('> ').context.m = msg;\n
\n

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

\n
$ node repl_test.js\n> m\n'message'\n

There are a few special REPL commands:

\n\n

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

\n\n", "miscs": [ { "textRaw": "Customizing Object displays in the REPL", "name": "customizing_object_displays_in_the_repl", "desc": "

The REPL module internally uses\nutil.inspect(), when printing values. However, util.inspect delegates the\n call to the object's inspect() function, if it has one. You can read more\n about this delegation here.

\n

For example, if you have defined an inspect() function on an object, like this:

\n
> var obj = {foo: 'this will not show up in the inspect() output'};\nundefined\n> obj.inspect = () => {\n...   return {bar: 'baz'};\n... };\n[Function]\n

and try to print obj in REPL, it will invoke the custom inspect() function:

\n
> obj\n{bar: 'baz'}\n
", "type": "misc", "displayName": "Customizing Object displays in the REPL" } ] } ], "classes": [ { "textRaw": "Class: REPLServer", "type": "class", "name": "REPLServer", "meta": { "added": [ "v0.1.91" ] }, "desc": "

This inherits from Readline Interface with the following events:

\n", "events": [ { "textRaw": "Event: 'exit'", "type": "event", "name": "exit", "meta": { "added": [ "v0.7.7" ] }, "desc": "

function () {}

\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

Example of listening for exit:

\n
replServer.on('exit', () => {\n  console.log('Got "exit" event from repl!');\n  process.exit();\n});\n
\n", "params": [] }, { "textRaw": "Event: 'reset'", "type": "event", "name": "reset", "meta": { "added": [ "v0.11.0" ] }, "desc": "

function (context) {}

\n

Emitted when the REPL's context is reset. This happens when you type .clear.\nIf you start the repl with { useGlobal: true } then this event will never\nbe emitted.

\n

Example of listening for reset:

\n
// Extend the initial repl context.\nvar replServer = repl.start({ options ... });\nsomeExtension.extend(r.context);\n\n// When a new context is created extend it as well.\nreplServer.on('reset', (context) => {\n  console.log('repl has a new context');\n  someExtension.extend(context);\n});\n
\n", "params": [] } ], "methods": [ { "textRaw": "replServer.defineCommand(keyword, cmd)", "type": "method", "name": "defineCommand", "meta": { "added": [ "v0.3.0" ] }, "signatures": [ { "params": [ { "textRaw": "`keyword` {String} ", "name": "keyword", "type": "String" }, { "textRaw": "`cmd` {Object|Function} ", "name": "cmd", "type": "Object|Function" } ] }, { "params": [ { "name": "keyword" }, { "name": "cmd" } ] } ], "desc": "

Makes a command available in the REPL. The command is invoked by typing a .\nfollowed by the keyword. The cmd is an object with the following values:

\n\n

If a function is provided instead of an object for cmd, it is treated as the\naction.

\n

Example of defining a command:

\n
// repl_test.js\nconst repl = require('repl');\n\nvar replServer = repl.start();\nreplServer.defineCommand('sayhello', {\n  help: 'Say hello',\n  action: function(name) {\n    this.write(`Hello, ${name}!\\n`);\n    this.displayPrompt();\n  }\n});\n
\n

Example of invoking that command from the REPL:

\n
> .sayhello Node.js User\nHello, Node.js User!\n
" }, { "textRaw": "replServer.displayPrompt([preserveCursor])", "type": "method", "name": "displayPrompt", "meta": { "added": [ "v0.1.91" ] }, "signatures": [ { "params": [ { "textRaw": "`preserveCursor` {Boolean} ", "name": "preserveCursor", "type": "Boolean", "optional": true } ] }, { "params": [ { "name": "preserveCursor", "optional": true } ] } ], "desc": "

Like readline.prompt except also adding indents with ellipses when inside\nblocks. The preserveCursor argument is passed to readline.prompt. This is\nused primarily with defineCommand. It's also used internally to render each\nprompt line.

\n" } ] } ], "methods": [ { "textRaw": "repl.start(options)", "type": "method", "name": "start", "desc": "

Returns and starts a REPLServer instance, that inherits from\nReadline Interface. Accepts an "options" Object that takes\nthe following values:

\n\n

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

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

On tab completion, eval will be called with .scope as an input string. It\nis expected to return an array of scope names to be used for the auto-completion.

\n

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

\n

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

\n
const net = require('net');\nconst repl = require('repl');\nvar connections = 0;\n\nrepl.start({\n  prompt: 'Node.js via stdin> ',\n  input: process.stdin,\n  output: process.stdout\n});\n\nnet.createServer((socket) => {\n  connections += 1;\n  repl.start({\n    prompt: 'Node.js via Unix socket> ',\n    input: socket,\n    output: socket\n  }).on('exit', () => {\n    socket.end();\n  })\n}).listen('/tmp/node-repl-sock');\n\nnet.createServer((socket) => {\n  connections += 1;\n  repl.start({\n    prompt: 'Node.js via TCP socket> ',\n    input: socket,\n    output: socket\n  }).on('exit', () => {\n    socket.end();\n  });\n}).listen(5001);\n
\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

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

\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

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

\n", "signatures": [ { "params": [ { "name": "options" } ] } ] } ], "type": "module", "displayName": "REPL" } ] }