2012-02-27 20:09:34 +01:00
|
|
|
# REPL
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-02-25 01:15:26 +01:00
|
|
|
Stability: 2 - Stable
|
2013-08-28 03:09:26 +02:00
|
|
|
|
2012-03-27 00:21:25 +02:00
|
|
|
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
|
2010-10-28 14:18:16 +02:00
|
|
|
just trying things out.
|
|
|
|
|
2015-02-07 11:25:13 +01:00
|
|
|
By executing `iojs` without any arguments from the command-line you will be
|
2010-10-28 14:18:16 +02:00
|
|
|
dropped into the REPL. It has simplistic emacs line-editing.
|
|
|
|
|
2015-02-07 11:25:13 +01:00
|
|
|
mjr:~$ iojs
|
2010-10-28 14:18:16 +02:00
|
|
|
Type '.help' for options.
|
2010-11-21 23:22:34 +01:00
|
|
|
> a = [ 1, 2, 3];
|
2010-10-28 14:18:16 +02:00
|
|
|
[ 1, 2, 3 ]
|
2010-11-21 23:22:34 +01:00
|
|
|
> a.forEach(function (v) {
|
2010-10-28 14:18:16 +02:00
|
|
|
... console.log(v);
|
|
|
|
... });
|
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
|
2015-02-07 11:25:13 +01:00
|
|
|
For advanced line-editors, start io.js with the environmental variable
|
2012-03-27 00:21:25 +02:00
|
|
|
`NODE_NO_READLINE=1`. This will start the main and debugger REPL in canonical
|
|
|
|
terminal settings which will allow you to use with `rlwrap`.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
For example, you could add this to your bashrc file:
|
|
|
|
|
2015-02-07 11:25:13 +01:00
|
|
|
alias iojs="env NODE_NO_READLINE=1 rlwrap iojs"
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-04-23 09:35:53 +02:00
|
|
|
The built-in repl (invoked by running `iojs` or `iojs -i`) may be controlled
|
|
|
|
via the following environment variables:
|
|
|
|
|
|
|
|
- `NODE_REPL_HISTORY_FILE` - if given, must be a path to a user-writable,
|
|
|
|
user-readable file. When a valid path is given, persistent history support
|
|
|
|
is enabled: REPL history will persist across `iojs` repl sessions.
|
|
|
|
- `NODE_REPL_HISTORY_SIZE` - defaults to `1000`. In conjunction with
|
|
|
|
`NODE_REPL_HISTORY_FILE`, controls how many lines of history will be
|
|
|
|
persisted. Must be a positive number.
|
|
|
|
- `NODE_REPL_MODE` - may be any of `sloppy`, `strict`, or `magic`. Defaults
|
|
|
|
to `magic`, which will automatically run "strict mode only" statements in
|
|
|
|
strict mode.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-03-27 00:21:25 +02:00
|
|
|
## repl.start(options)
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2014-02-15 15:21:26 +01:00
|
|
|
Returns and starts a `REPLServer` instance, that inherits from
|
|
|
|
[Readline Interface][]. Accepts an "options" Object that takes
|
|
|
|
the following values:
|
2011-10-11 21:14:00 +02:00
|
|
|
|
2012-03-27 00:21:25 +02:00
|
|
|
- `prompt` - the prompt and `stream` for all I/O. Defaults to `> `.
|
2011-10-20 19:00:15 +02:00
|
|
|
|
2012-03-27 00:21:25 +02:00
|
|
|
- `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`.
|
|
|
|
|
2012-03-28 03:00:59 +02:00
|
|
|
- `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.
|
|
|
|
|
2012-03-27 00:21:25 +02:00
|
|
|
- `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`.
|
2011-11-02 23:38:38 +01:00
|
|
|
|
2012-03-28 02:39:14 +02:00
|
|
|
- `writer` - the function to invoke for each command that gets evaluated which
|
|
|
|
returns the formatting (including coloring) to display. Defaults to
|
|
|
|
`util.inspect`.
|
|
|
|
|
2015-04-23 09:35:53 +02:00
|
|
|
- `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.
|
|
|
|
|
2011-10-11 21:14:00 +02:00
|
|
|
You can use your own `eval` function if it has following signature:
|
|
|
|
|
2012-03-06 20:19:30 +01:00
|
|
|
function eval(cmd, context, filename, callback) {
|
2011-10-11 21:14:00 +02:00
|
|
|
callback(null, result);
|
|
|
|
}
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-05-12 10:00:35 +02:00
|
|
|
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.
|
|
|
|
|
2015-02-07 11:25:13 +01:00
|
|
|
Multiple REPLs may be started against the same running instance of io.js. Each
|
2010-10-28 14:18:16 +02:00
|
|
|
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:
|
|
|
|
|
|
|
|
var net = require("net"),
|
|
|
|
repl = require("repl");
|
|
|
|
|
|
|
|
connections = 0;
|
|
|
|
|
2012-03-27 00:21:25 +02:00
|
|
|
repl.start({
|
2015-02-07 11:25:13 +01:00
|
|
|
prompt: "io.js via stdin> ",
|
2012-03-27 00:21:25 +02:00
|
|
|
input: process.stdin,
|
|
|
|
output: process.stdout
|
|
|
|
});
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
net.createServer(function (socket) {
|
|
|
|
connections += 1;
|
2012-03-27 00:21:25 +02:00
|
|
|
repl.start({
|
2015-02-07 11:25:13 +01:00
|
|
|
prompt: "io.js via Unix socket> ",
|
2012-03-27 00:21:25 +02:00
|
|
|
input: socket,
|
|
|
|
output: socket
|
|
|
|
}).on('exit', function() {
|
|
|
|
socket.end();
|
|
|
|
})
|
2015-02-07 11:25:13 +01:00
|
|
|
}).listen("/tmp/iojs-repl-sock");
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
net.createServer(function (socket) {
|
|
|
|
connections += 1;
|
2012-03-27 00:21:25 +02:00
|
|
|
repl.start({
|
2015-02-07 11:25:13 +01:00
|
|
|
prompt: "io.js via TCP socket> ",
|
2012-03-27 00:21:25 +02:00
|
|
|
input: socket,
|
|
|
|
output: socket
|
|
|
|
}).on('exit', function() {
|
|
|
|
socket.end();
|
|
|
|
});
|
2010-10-28 14:18:16 +02:00
|
|
|
}).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.
|
|
|
|
|
2010-11-21 23:22:34 +01:00
|
|
|
By starting a REPL from a Unix socket-based server instead of stdin, you can
|
2015-02-07 11:25:13 +01:00
|
|
|
connect to a long-running io.js process without restarting it.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2012-03-27 00:21:25 +02:00
|
|
|
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
|
|
|
|
|
2012-03-13 19:50:28 +01:00
|
|
|
### Event: 'exit'
|
|
|
|
|
|
|
|
`function () {}`
|
|
|
|
|
|
|
|
Emitted when the user exits the REPL in any of the defined ways. Namely, typing
|
2012-03-27 21:41:42 +02:00
|
|
|
`.exit` at the repl, pressing Ctrl+C twice to signal SIGINT, or pressing Ctrl+D
|
|
|
|
to signal "end" on the `input` stream.
|
2012-03-13 19:50:28 +01:00
|
|
|
|
|
|
|
Example of listening for `exit`:
|
|
|
|
|
|
|
|
r.on('exit', function () {
|
|
|
|
console.log('Got "exit" event from repl!');
|
|
|
|
process.exit();
|
|
|
|
});
|
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2013-03-14 21:49:14 +01:00
|
|
|
### 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`:
|
|
|
|
|
|
|
|
// Extend the initial repl context.
|
|
|
|
r = repl.start({ options ... });
|
|
|
|
someExtension.extend(r.context);
|
|
|
|
|
|
|
|
// When a new context is created extend it as well.
|
|
|
|
r.on('reset', function (context) {
|
|
|
|
console.log('repl has a new context');
|
|
|
|
someExtension.extend(context);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2012-02-27 20:09:34 +01:00
|
|
|
## REPL Features
|
|
|
|
|
|
|
|
<!-- type=misc -->
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
Inside the REPL, Control+D will exit. Multi-line expressions can be input.
|
2011-11-13 02:57:42 +01:00
|
|
|
Tab completion is supported for both global and local variables.
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-03-24 22:06:27 +01:00
|
|
|
Core modules will be loaded on-demand into the environment. For example,
|
|
|
|
accessing `fs` will `require()` the `fs` module as `global.fs`.
|
|
|
|
|
2010-10-28 14:18:16 +02:00
|
|
|
The special variable `_` (underscore) contains the result of the last expression.
|
|
|
|
|
2010-11-21 23:22:34 +01:00
|
|
|
> [ "a", "b", "c" ]
|
2010-10-28 14:18:16 +02:00
|
|
|
[ 'a', 'b', 'c' ]
|
2010-11-21 23:22:34 +01:00
|
|
|
> _.length
|
2010-10-28 14:18:16 +02:00
|
|
|
3
|
2010-11-21 23:22:34 +01:00
|
|
|
> _ += 1
|
2010-10-28 14:18:16 +02:00
|
|
|
4
|
|
|
|
|
2011-10-20 17:54:50 +02:00
|
|
|
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:
|
|
|
|
|
|
|
|
// repl_test.js
|
|
|
|
var repl = require("repl"),
|
|
|
|
msg = "message";
|
|
|
|
|
2013-02-12 21:04:23 +01:00
|
|
|
repl.start("> ").context.m = msg;
|
2011-10-20 17:54:50 +02:00
|
|
|
|
|
|
|
Things in the `context` object appear as local within the REPL:
|
|
|
|
|
2015-02-07 11:25:13 +01:00
|
|
|
mjr:~$ iojs repl_test.js
|
2011-10-20 17:54:50 +02:00
|
|
|
> m
|
|
|
|
'message'
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
There are a few special REPL commands:
|
|
|
|
|
2010-11-18 13:00:24 +01:00
|
|
|
- `.break` - While inputting a multi-line expression, sometimes you get lost
|
|
|
|
or just don't care about completing it. `.break` will start over.
|
2011-10-20 17:54:50 +02:00
|
|
|
- `.clear` - Resets the `context` object to an empty object and clears any
|
|
|
|
multi-line expression.
|
2010-10-28 14:18:16 +02:00
|
|
|
- `.exit` - Close the I/O stream, which will cause the REPL to exit.
|
|
|
|
- `.help` - Show this list of special commands.
|
2011-11-13 02:57:42 +01:00
|
|
|
- `.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
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2011-04-21 21:17:21 +02:00
|
|
|
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.
|
2015-05-12 10:00:35 +02:00
|
|
|
- `<tab>` - Show both global and local(scope) variables
|
2011-04-21 21:17:21 +02:00
|
|
|
|
2015-07-09 20:54:21 +02:00
|
|
|
|
|
|
|
### Customizing Object displays in the REPL
|
|
|
|
|
|
|
|
The REPL module internally uses
|
|
|
|
[util.inspect()][], when printing values. However, `util.inspect` delegates the
|
|
|
|
call to the object's `inspect()` function, if it has one. You can read more
|
|
|
|
about this delegation [here][].
|
|
|
|
|
|
|
|
For example, if you have defined an `inspect()` function on an object, like this:
|
|
|
|
|
|
|
|
> var obj = { foo: 'this will not show up in the inspect() output' };
|
|
|
|
undefined
|
|
|
|
> obj.inspect = function() {
|
|
|
|
... return { bar: 'baz' };
|
|
|
|
... };
|
|
|
|
[Function]
|
|
|
|
|
|
|
|
and try to print `obj` in REPL, it will invoke the custom `inspect()` function:
|
|
|
|
|
|
|
|
> obj
|
|
|
|
{ bar: 'baz' }
|
|
|
|
|
|
|
|
[util.inspect()]: util.html#util_util_inspect_object_options
|
|
|
|
[here]: util.html#util_custom_inspect_function_on_objects
|