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-08-13 18:14:34 +02:00
|
|
|
By executing `node` 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.
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2016-01-21 23:21:22 +01:00
|
|
|
$ node
|
2016-01-17 18:39:07 +01:00
|
|
|
Type '.help' for options.
|
2016-04-28 17:13:38 +02:00
|
|
|
> a = [1, 2, 3];
|
2016-01-17 18:39:07 +01:00
|
|
|
[ 1, 2, 3 ]
|
2016-01-24 10:15:51 +01:00
|
|
|
> a.forEach((v) => {
|
2016-01-17 18:39:07 +01:00
|
|
|
... console.log(v);
|
|
|
|
... });
|
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-08-13 18:14:34 +02:00
|
|
|
For advanced line-editors, start Node.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:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
|
|
|
alias node="env NODE_NO_READLINE=1 rlwrap node"
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-11-04 18:38:09 +01:00
|
|
|
## Environment Variable Options
|
|
|
|
|
|
|
|
The built-in repl (invoked by running `node` or `node -i`) may be controlled
|
|
|
|
via the following environment variables:
|
|
|
|
|
|
|
|
- `NODE_REPL_HISTORY` - When a valid path is given, persistent REPL history
|
|
|
|
will be saved to the specified file rather than `.node_repl_history` in the
|
|
|
|
user's home directory. Setting this value to `""` will disable persistent
|
2016-01-05 13:28:27 +01:00
|
|
|
REPL history. Whitespace will be trimmed from the value.
|
2016-02-11 07:11:26 +01:00
|
|
|
- `NODE_REPL_HISTORY_SIZE` - Defaults to `1000`. Controls how many lines of
|
2015-11-04 18:38:09 +01:00
|
|
|
history will be persisted if history is available. Must be a positive number.
|
2016-02-11 07:11:26 +01:00
|
|
|
- `NODE_REPL_MODE` - May be any of `sloppy`, `strict`, or `magic`. Defaults
|
2015-11-04 18:38:09 +01:00
|
|
|
to `magic`, which will automatically run "strict mode only" statements in
|
|
|
|
strict mode.
|
|
|
|
|
2015-08-04 22:59:23 +02:00
|
|
|
## Persistent History
|
2015-08-04 08:33:48 +02:00
|
|
|
|
2015-08-13 18:14:34 +02:00
|
|
|
By default, the REPL will persist history between `node` REPL sessions by saving
|
2015-08-04 08:33:48 +02:00
|
|
|
to a `.node_repl_history` file in the user's home directory. This can be
|
|
|
|
disabled by setting the environment variable `NODE_REPL_HISTORY=""`.
|
|
|
|
|
2015-08-20 00:37:52 +02:00
|
|
|
### NODE_REPL_HISTORY_FILE
|
|
|
|
|
|
|
|
Stability: 0 - Deprecated: Use `NODE_REPL_HISTORY` instead.
|
|
|
|
|
2015-08-13 18:14:34 +02:00
|
|
|
Previously in Node.js/io.js v2.x, REPL history was controlled by using a
|
2015-08-04 08:33:48 +02:00
|
|
|
`NODE_REPL_HISTORY_FILE` environment variable, and the history was saved in JSON
|
|
|
|
format. This variable has now been deprecated, and your REPL history will
|
|
|
|
automatically be converted to using plain text. The new file will be saved to
|
|
|
|
either your home directory, or a directory defined by the `NODE_REPL_HISTORY`
|
2016-01-05 11:49:54 +01:00
|
|
|
variable, as documented [here](#repl_environment_variable_options).
|
2015-08-04 08:33:48 +02:00
|
|
|
|
2015-11-04 18:38:09 +01:00
|
|
|
## REPL Features
|
2015-08-04 08:33:48 +02:00
|
|
|
|
2015-11-04 18:38:09 +01:00
|
|
|
<!-- type=misc -->
|
2015-04-23 09:35:53 +02:00
|
|
|
|
2015-11-04 18:38:09 +01:00
|
|
|
Inside the REPL, Control+D will exit. Multi-line expressions can be input.
|
|
|
|
Tab completion is supported for both global and local variables.
|
|
|
|
|
|
|
|
Core modules will be loaded on-demand into the environment. For example,
|
|
|
|
accessing `fs` will `require()` the `fs` module as `global.fs`.
|
|
|
|
|
|
|
|
The special variable `_` (underscore) contains the result of the last expression.
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
|
|
|
> [ 'a', 'b', 'c' ]
|
|
|
|
[ 'a', 'b', 'c' ]
|
|
|
|
> _.length
|
|
|
|
3
|
|
|
|
> _ += 1
|
|
|
|
4
|
|
|
|
```
|
2015-11-04 18:38:09 +01:00
|
|
|
|
2016-03-02 22:45:16 +01:00
|
|
|
Explicitly setting `_` will disable this behavior until the context is reset.
|
|
|
|
|
2015-11-04 18:38:09 +01: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:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
// repl_test.js
|
|
|
|
const repl = require('repl');
|
|
|
|
var msg = 'message';
|
2015-11-04 18:38:09 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
repl.start('> ').context.m = msg;
|
|
|
|
```
|
2015-11-04 18:38:09 +01:00
|
|
|
|
|
|
|
Things in the `context` object appear as local within the REPL:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2016-01-21 23:21:22 +01:00
|
|
|
$ node repl_test.js
|
2016-01-17 18:39:07 +01:00
|
|
|
> m
|
|
|
|
'message'
|
|
|
|
```
|
2015-11-04 18:38:09 +01:00
|
|
|
|
|
|
|
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.
|
|
|
|
- `<tab>` - Show both global and local(scope) variables
|
|
|
|
|
|
|
|
|
|
|
|
### Customizing Object displays in the REPL
|
|
|
|
|
|
|
|
The REPL module internally uses
|
2015-11-28 00:30:32 +01:00
|
|
|
[`util.inspect()`][], when printing values. However, `util.inspect` delegates the
|
2015-11-04 18:38:09 +01:00
|
|
|
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:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
|
|
|
> var obj = {foo: 'this will not show up in the inspect() output'};
|
|
|
|
undefined
|
2016-01-24 10:15:51 +01:00
|
|
|
> obj.inspect = () => {
|
2016-01-17 18:39:07 +01:00
|
|
|
... return {bar: 'baz'};
|
|
|
|
... };
|
|
|
|
[Function]
|
|
|
|
```
|
2015-11-04 18:38:09 +01:00
|
|
|
|
|
|
|
and try to print `obj` in REPL, it will invoke the custom `inspect()` function:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
|
|
|
> obj
|
|
|
|
{bar: 'baz'}
|
|
|
|
```
|
2015-11-04 18:38:09 +01:00
|
|
|
|
2015-11-16 18:11:50 +01:00
|
|
|
## Class: REPLServer
|
|
|
|
|
|
|
|
This inherits from [Readline Interface][] with the following events:
|
|
|
|
|
|
|
|
### Event: 'exit'
|
|
|
|
|
|
|
|
`function () {}`
|
|
|
|
|
|
|
|
Emitted when the user exits the REPL in any of the defined ways. Namely, typing
|
2015-11-28 00:30:32 +01:00
|
|
|
`.exit` at the repl, pressing Ctrl+C twice to signal `SIGINT`, or pressing Ctrl+D
|
|
|
|
to signal `'end'` on the `input` stream.
|
2015-11-16 18:11:50 +01:00
|
|
|
|
|
|
|
Example of listening for `exit`:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
replServer.on('exit', () => {
|
|
|
|
console.log('Got "exit" event from repl!');
|
|
|
|
process.exit();
|
|
|
|
});
|
|
|
|
```
|
2015-11-16 18:11:50 +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`:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
// Extend the initial repl context.
|
|
|
|
var replServer = repl.start({ options ... });
|
|
|
|
someExtension.extend(r.context);
|
2015-11-16 18:11:50 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
// When a new context is created extend it as well.
|
|
|
|
replServer.on('reset', (context) => {
|
|
|
|
console.log('repl has a new context');
|
|
|
|
someExtension.extend(context);
|
|
|
|
});
|
|
|
|
```
|
2015-11-16 18:11:50 +01:00
|
|
|
|
|
|
|
### replServer.defineCommand(keyword, cmd)
|
|
|
|
|
|
|
|
* `keyword` {String}
|
|
|
|
* `cmd` {Object|Function}
|
|
|
|
|
|
|
|
Makes a command available in the REPL. The command is invoked by typing a `.`
|
|
|
|
followed by the keyword. The `cmd` is an object with the following values:
|
|
|
|
|
|
|
|
- `help` - help text to be displayed when `.help` is entered (Optional).
|
|
|
|
- `action` - a function to execute, potentially taking in a string argument,
|
|
|
|
when the command is invoked, bound to the REPLServer instance (Required).
|
|
|
|
|
|
|
|
If a function is provided instead of an object for `cmd`, it is treated as the
|
|
|
|
`action`.
|
|
|
|
|
|
|
|
Example of defining a command:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
// repl_test.js
|
|
|
|
const repl = require('repl');
|
2015-11-16 18:11:50 +01:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
var replServer = repl.start();
|
|
|
|
replServer.defineCommand('sayhello', {
|
|
|
|
help: 'Say hello',
|
|
|
|
action: function(name) {
|
|
|
|
this.write(`Hello, ${name}!\n`);
|
|
|
|
this.displayPrompt();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
```
|
2015-11-16 18:11:50 +01:00
|
|
|
|
|
|
|
Example of invoking that command from the REPL:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
|
|
|
> .sayhello Node.js User
|
|
|
|
Hello, Node.js User!
|
|
|
|
```
|
2015-11-16 18:11:50 +01:00
|
|
|
|
|
|
|
### replServer.displayPrompt([preserveCursor])
|
|
|
|
|
|
|
|
* `preserveCursor` {Boolean}
|
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
Like [`readline.prompt`][] except also adding indents with ellipses when inside
|
|
|
|
blocks. The `preserveCursor` argument is passed to [`readline.prompt`][]. This is
|
2015-11-16 18:11:50 +01:00
|
|
|
used primarily with `defineCommand`. It's also used internally to render each
|
|
|
|
prompt line.
|
|
|
|
|
2016-02-23 16:08:30 +01:00
|
|
|
## repl.start([options])
|
2010-10-28 14:18:16 +02:00
|
|
|
|
2015-08-04 08:33:48 +02:00
|
|
|
Returns and starts a `REPLServer` instance, that inherits from
|
|
|
|
[Readline Interface][]. Accepts an "options" Object that takes
|
2014-02-15 15:21:26 +01:00
|
|
|
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
|
|
|
|
2016-02-11 07:11:26 +01:00
|
|
|
On tab completion, `eval` will be called with `.scope` as an input string. It
|
2015-05-12 10:00:35 +02:00
|
|
|
is expected to return an array of scope names to be used for the auto-completion.
|
|
|
|
|
2015-08-13 18:14:34 +02:00
|
|
|
Multiple REPLs may be started against the same running instance of Node.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:
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
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);
|
|
|
|
```
|
2010-10-28 14:18:16 +02:00
|
|
|
|
|
|
|
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-08-13 18:14:34 +02:00
|
|
|
connect to a long-running Node.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
|
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
[`readline.prompt`]: readline.html#readline_rl_prompt_preservecursor
|
|
|
|
[`util.inspect()`]: util.html#util_util_inspect_object_options
|
2015-11-11 07:33:28 +01:00
|
|
|
[here]: util.html#util_custom_inspect_function_on_objects
|
2015-11-28 00:30:32 +01:00
|
|
|
[Readline Interface]: readline.html#readline_class_interface
|