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 andstream
for all I/O. Defaults to>
. -
input
- the readable stream to listen to. Defaults toprocess.stdin
. -
output
- the writable stream to write readline data to. Defaults toprocess.stdout
. -
terminal
- passtrue
if thestream
should be treated like a TTY, and have ANSI/VT100 escape codes written to it. Defaults to checkingisTTY
on theoutput
stream upon instantiation. -
eval
- function that will be used to eval each given line. Defaults to an async wrapper foreval()
. See below for an example of a customeval
. -
useColors
- a boolean which specifies whether or not thewriter
function should output colors. If a differentwriter
function is set then this does nothing. Defaults to the repl'sterminal
value. -
useGlobal
- if set totrue
, then the repl will use theglobal
object, instead of running scripts in a separate context. Defaults tofalse
. -
ignoreUndefined
- if set totrue
, then the repl will not output the return value of command if it'sundefined
. Defaults tofalse
. -
writer
- the function to invoke for each command that gets evaluated which returns the formatting (including coloring) to display. Defaults toutil.inspect
. -
replMode
- controls whether the repl runs all commands in strict mode, default mode, or a hybrid mode ("magic" mode.) Acceptable values are:-
repl.REPL_MODE_SLOPPY
- run commands in sloppy mode. -
repl.REPL_MODE_STRICT
- run commands in strict mode. This is equivalent to prefacing every repl statement with'use strict'
. -
repl.REPL_MODE_MAGIC
- attempt to run commands in default mode. If they fail to parse, re-try in strict mode.
-
You can use your own eval
function if it has following signature:
function eval(cmd, context, filename, callback) { callback(null, result); }
On tab completion, eval
will be called with .scope
as an input string. It is expected to return an array of scope names to be used for the auto-completion.
Multiple REPLs may be started against the same running instance of Node.js. 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:
const net = require('net'); const repl = require('repl'); var connections = 0; repl.start({ prompt: 'Node.js via stdin> ', input: process.stdin, output: process.stdout }); net.createServer((socket) => { connections += 1; repl.start({ prompt: 'Node.js via Unix socket> ', input: socket, output: socket }).on('exit', () => { socket.end(); }) }).listen('/tmp/node-repl-sock'); net.createServer((socket) => { connections += 1; repl.start({ prompt: 'Node.js via TCP socket> ', input: socket, output: socket }).on('exit', () => { socket.end(); }); }).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.js 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
Please login to continue.