REPL

  1. Stability: 3 - Stable

A Read-Eval-Print-Loop (REPL) is available both as a standalone program and
easily includable in other programs. The 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.

  1. mjr:~$ node
  2. Type '.help' for options.
  3. > a = [ 1, 2, 3];
  4. [ 1, 2, 3 ]
  5. > a.forEach(function (v) {
  6. ... console.log(v);
  7. ... });
  8. 1
  9. 2
  10. 3

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

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

  1. alias node="env NODE_NO_READLINE=1 rlwrap node"

repl.start(options)

Returns and starts a REPLServer instance, that inherits from
[Readline Interface][]. Accepts an “options” Object that takes
the following values:

  • prompt - the prompt and stream for all I/O. Defaults to >.

  • input - the readable stream to listen to. Defaults to process.stdin.

  • output - the writable stream to write readline data to. Defaults to
    process.stdout.

  • terminal - pass true if the stream should be treated like a TTY, and
    have ANSI/VT100 escape codes written to it. Defaults to checking isTTY
    on the output stream upon instantiation.

  • eval - function that will be used to eval each given line. Defaults to
    an async wrapper for eval(). See below for an example of a custom eval.

  • useColors - a boolean which specifies whether or not the writer function
    should output colors. If a different writer function is set then this does
    nothing. Defaults to the repl’s terminal value.

  • useGlobal - if set to true, then the repl will use the global object,
    instead of running scripts in a separate context. Defaults to false.

  • ignoreUndefined - if set to true, then the repl will not output the
    return value of command if it’s undefined. Defaults to false.

  • writer - the function to invoke for each command that gets evaluated which
    returns the formatting (including coloring) to display. Defaults to
    util.inspect.

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

  1. function eval(cmd, context, filename, callback) {
  2. callback(null, result);
  3. }

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:

  1. var net = require("net"),
  2. repl = require("repl");
  3. connections = 0;
  4. repl.start({
  5. prompt: "node via stdin> ",
  6. input: process.stdin,
  7. output: process.stdout
  8. });
  9. net.createServer(function (socket) {
  10. connections += 1;
  11. repl.start({
  12. prompt: "node via Unix socket> ",
  13. input: socket,
  14. output: socket
  15. }).on('exit', function() {
  16. socket.end();
  17. })
  18. }).listen("/tmp/node-repl-sock");
  19. net.createServer(function (socket) {
  20. connections += 1;
  21. repl.start({
  22. prompt: "node via TCP socket> ",
  23. input: socket,
  24. output: socket
  25. }).on('exit', function() {
  26. socket.end();
  27. });
  28. }).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. telnet is useful
for connecting to TCP sockets, and socat can 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.

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

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

Event: ‘exit’

function () {}

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

Example of listening for exit:

  1. r.on('exit', function () {
  2. console.log('Got "exit" event from repl!');
  3. process.exit();
  4. });

Event: ‘reset’

function (context) {}

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

Example of listening for reset:

  1. // Extend the initial repl context.
  2. r = repl.start({ options ... });
  3. someExtension.extend(r.context);
  4. // When a new context is created extend it as well.
  5. r.on('reset', function (context) {
  6. console.log('repl has a new context');
  7. someExtension.extend(context);
  8. });

REPL Features

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

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

  1. > [ "a", "b", "c" ]
  2. [ 'a', 'b', 'c' ]
  3. > _.length
  4. 3
  5. > _ += 1
  6. 4

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

  1. // repl_test.js
  2. var repl = require("repl"),
  3. msg = "message";
  4. repl.start("> ").context.m = msg;

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

  1. mjr:~$ node repl_test.js
  2. > m
  3. 'message'

There are a few special REPL commands:

  • .break - While inputting a multi-line expression, sometimes you get lost
    or just don’t care about completing it. .break will start over.
  • .clear - Resets the context object to an empty object and clears any
    multi-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

    .save ./file/to/save.js

  • .load - Load a file into the current REPL session.

    .load ./file/to/load.js

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

  • <ctrl>C - Similar to the .break keyword. Terminates the current
    command. Press twice on a blank line to forcibly exit.
  • <ctrl>D - Similar to the .exit keyword.