Skip to content

Latest commit

Β 

History

History
571 lines (456 loc) Β· 17.7 KB

File metadata and controls

571 lines (456 loc) Β· 17.7 KB
Β 
Feb 27, 2012
Feb 27, 2012
1
# REPL
Oct 28, 2010
Oct 28, 2010
2
Aug 4, 2016
Aug 4, 2016
3
> Stability: 2 - Stable
Aug 28, 2013
Aug 28, 2013
4
Jun 6, 2016
Jun 6, 2016
5
The `repl` module provides a Read-Eval-Print-Loop (REPL) implementation that
Nov 18, 2016
Nov 18, 2016
6
is available both as a standalone program or includible in other applications.
Jun 6, 2016
Jun 6, 2016
7
It can be accessed using:
Oct 28, 2010
Oct 28, 2010
8
Jun 6, 2016
Jun 6, 2016
9
```js
10
const repl = require('repl');
Jan 21, 2016
Jan 21, 2016
11
```
Jun 6, 2016
Jun 6, 2016
12
13
## Design and Features
14
15
The `repl` module exports the `repl.REPLServer` class. While running, instances
16
of `repl.REPLServer` will accept individual lines of user input, evaluate those
17
according to a user-defined evaluation function, then output the result. Input
18
and output may be from `stdin` and `stdout`, respectively, or may be connected
19
to any Node.js [stream][].
20
21
Instances of `repl.REPLServer` support automatic completion of inputs,
22
simplistic Emacs-style line editing, multi-line inputs, ANSI-styled output,
23
saving and restoring current REPL session state, error recovery, and
24
customizable evaluation functions.
25
26
### Commands and Special Keys
27
28
The following special commands are supported by all REPL instances:
29
30
* `.break` - When in the process of inputting a multi-line expression, entering
31
the `.break` command (or pressing the `<ctrl>-C` key combination) will abort
32
further input or processing of that expression.
33
* `.clear` - Resets the REPL `context` to an empty object and clears any
34
multi-line expression currently being input.
35
* `.exit` - Close the I/O stream, causing the REPL to exit.
36
* `.help` - Show this list of special commands.
37
* `.save` - Save the current REPL session to a file:
38
`> .save ./file/to/save.js`
39
* `.load` - Load a file into the current REPL session.
40
`> .load ./file/to/load.js`
Aug 6, 2016
Aug 6, 2016
41
* `.editor` - Enter editor mode (`<ctrl>-D` to finish, `<ctrl>-C` to cancel)
42
Apr 24, 2017
Apr 24, 2017
43
<!-- eslint-disable -->
Aug 6, 2016
Aug 6, 2016
44
```js
45
> .editor
46
// Entering editor mode (^D to finish, ^C to cancel)
47
function welcome(name) {
48
return `Hello ${name}!`;
49
}
50
51
welcome('Node.js User');
52
53
// ^D
54
'Hello Node.js User!'
55
>
56
```
Jun 6, 2016
Jun 6, 2016
57
58
The following key combinations in the REPL have these special effects:
59
60
* `<ctrl>-C` - When pressed once, has the same effect as the `.break` command.
61
When pressed twice on a blank line, has the same effect as the `.exit`
62
command.
63
* `<ctrl>-D` - Has the same effect as the `.exit` command.
64
* `<tab>` - When pressed on a blank line, displays global and local(scope)
65
variables. When pressed while entering other input, displays relevant
66
autocompletion options.
67
68
### Default Evaluation
69
70
By default, all instances of `repl.REPLServer` use an evaluation function that
71
evaluates JavaScript expressions and provides access to Node.js' built-in
72
modules. This default behavior can be overridden by passing in an alternative
73
evaluation function when the `repl.REPLServer` instance is created.
74
75
#### JavaScript Expressions
76
77
The default evaluator supports direct evaluation of JavaScript expressions:
78
Apr 24, 2017
Apr 24, 2017
79
<!-- eslint-disable -->
Jun 6, 2016
Jun 6, 2016
80
```js
81
> 1 + 1
Jan 21, 2016
Jan 21, 2016
82
2
Dec 25, 2016
Dec 25, 2016
83
> const m = 2
Jun 6, 2016
Jun 6, 2016
84
undefined
85
> m + 1
Jan 21, 2016
Jan 21, 2016
86
3
87
```
Oct 28, 2010
Oct 28, 2010
88
Dec 25, 2016
Dec 25, 2016
89
Unless otherwise scoped within blocks or functions, variables declared
Dec 25, 2016
Dec 25, 2016
90
either implicitly or using the `const`, `let`, or `var` keywords
Dec 25, 2016
Dec 25, 2016
91
are declared at the global scope.
Oct 28, 2010
Oct 28, 2010
92
Jun 6, 2016
Jun 6, 2016
93
#### Global and Local Scope
Oct 28, 2010
Oct 28, 2010
94
Jun 6, 2016
Jun 6, 2016
95
The default evaluator provides access to any variables that exist in the global
96
scope. It is possible to expose a variable to the REPL explicitly by assigning
97
it to the `context` object associated with each `REPLServer`. For example:
Nov 13, 2015
Nov 13, 2015
98
Jun 6, 2016
Jun 6, 2016
99
```js
100
const repl = require('repl');
Dec 25, 2016
Dec 25, 2016
101
const msg = 'message';
Nov 13, 2015
Nov 13, 2015
102
Jun 6, 2016
Jun 6, 2016
103
repl.start('> ').context.m = msg;
104
```
Nov 13, 2015
Nov 13, 2015
105
Jun 6, 2016
Jun 6, 2016
106
Properties in the `context` object appear as local within the REPL:
Aug 4, 2015
Aug 4, 2015
107
Apr 24, 2017
Apr 24, 2017
108
<!-- eslint-disable -->
Jun 6, 2016
Jun 6, 2016
109
```js
110
$ node repl_test.js
111
> m
112
'message'
113
```
Aug 4, 2015
Aug 4, 2015
114
Jun 6, 2016
Jun 6, 2016
115
It is important to note that context properties are *not* read-only by default.
116
To specify read-only globals, context properties must be defined using
117
`Object.defineProperty()`:
Aug 24, 2015
Aug 24, 2015
118
Jun 6, 2016
Jun 6, 2016
119
```js
120
const repl = require('repl');
Dec 25, 2016
Dec 25, 2016
121
const msg = 'message';
Aug 24, 2015
Aug 24, 2015
122
Jun 6, 2016
Jun 6, 2016
123
const r = repl.start('> ');
Sep 12, 2016
Sep 12, 2016
124
Object.defineProperty(r.context, 'm', {
Jun 6, 2016
Jun 6, 2016
125
configurable: false,
126
enumerable: true,
127
value: msg
128
});
129
```
Aug 4, 2015
Aug 4, 2015
130
Jun 6, 2016
Jun 6, 2016
131
#### Accessing Core Node.js Modules
Aug 4, 2015
Aug 4, 2015
132
Jun 6, 2016
Jun 6, 2016
133
The default evaluator will automatically load Node.js core modules into the
134
REPL environment when used. For instance, unless otherwise declared as a
135
global or scoped variable, the input `fs` will be evaluated on-demand as
136
`global.fs = require('fs')`.
May 1, 2015
May 1, 2015
137
Apr 24, 2017
Apr 24, 2017
138
<!-- eslint-disable -->
Jun 6, 2016
Jun 6, 2016
139
```js
140
> fs.createReadStream('./some/file');
141
```
Nov 13, 2015
Nov 13, 2015
142
Jun 6, 2016
Jun 6, 2016
143
#### Assignment of the `_` (underscore) variable
Nov 13, 2015
Nov 13, 2015
144
Jun 6, 2016
Jun 6, 2016
145
The default evaluator will, by default, assign the result of the most recently
146
evaluated expression to the special variable `_` (underscore).
Dec 25, 2016
Dec 25, 2016
147
Explicitly setting `_` to a value will disable this behavior.
Nov 13, 2015
Nov 13, 2015
148
Apr 24, 2017
Apr 24, 2017
149
<!-- eslint-disable -->
Jun 6, 2016
Jun 6, 2016
150
```js
Jan 21, 2016
Jan 21, 2016
151
> [ 'a', 'b', 'c' ]
152
[ 'a', 'b', 'c' ]
153
> _.length
154
3
155
> _ += 1
Dec 25, 2016
Dec 25, 2016
156
Expression assignment to _ now disabled.
157
4
158
> 1 + 1
159
2
160
> _
Jan 21, 2016
Jan 21, 2016
161
4
162
```
Nov 13, 2015
Nov 13, 2015
163
Jun 6, 2016
Jun 6, 2016
164
### Custom Evaluation Functions
165
166
When a new `repl.REPLServer` is created, a custom evaluation function may be
167
provided. This can be used, for instance, to implement fully customized REPL
168
applications.
Mar 18, 2016
Mar 18, 2016
169
Jun 6, 2016
Jun 6, 2016
170
The following illustrates a hypothetical example of a REPL that performs
171
translation of text from one language to another:
Nov 13, 2015
Nov 13, 2015
172
Jan 21, 2016
Jan 21, 2016
173
```js
174
const repl = require('repl');
Jun 6, 2016
Jun 6, 2016
175
const Translator = require('translator').Translator;
Nov 13, 2015
Nov 13, 2015
176
Jun 6, 2016
Jun 6, 2016
177
const myTranslator = new Translator('en', 'fr');
Nov 13, 2015
Nov 13, 2015
178
Jun 6, 2016
Jun 6, 2016
179
function myEval(cmd, context, filename, callback) {
180
callback(null, myTranslator.translate(cmd));
181
}
Nov 13, 2015
Nov 13, 2015
182
Jun 6, 2016
Jun 6, 2016
183
repl.start({prompt: '> ', eval: myEval});
Jan 21, 2016
Jan 21, 2016
184
```
Nov 13, 2015
Nov 13, 2015
185
Jun 6, 2016
Jun 6, 2016
186
#### Recoverable Errors
Nov 13, 2015
Nov 13, 2015
187
Jun 6, 2016
Jun 6, 2016
188
As a user is typing input into the REPL prompt, pressing the `<enter>` key will
189
send the current line of input to the `eval` function. In order to support
190
multi-line input, the eval function can return an instance of `repl.Recoverable`
191
to the provided callback function:
Nov 13, 2015
Nov 13, 2015
192
Jun 6, 2016
Jun 6, 2016
193
```js
Dec 25, 2016
Dec 25, 2016
194
function myEval(cmd, context, filename, callback) {
Dec 25, 2016
Dec 25, 2016
195
let result;
Jun 6, 2016
Jun 6, 2016
196
try {
197
result = vm.runInThisContext(cmd);
198
} catch (e) {
199
if (isRecoverableError(e)) {
200
return callback(new repl.Recoverable(e));
201
}
202
}
203
callback(null, result);
204
}
Nov 13, 2015
Nov 13, 2015
205
Jun 6, 2016
Jun 6, 2016
206
function isRecoverableError(error) {
207
if (error.name === 'SyntaxError') {
208
return /^(Unexpected end of input|Unexpected token)/.test(error.message);
209
}
210
return false;
211
}
212
```
Nov 13, 2015
Nov 13, 2015
213
Jun 6, 2016
Jun 6, 2016
214
### Customizing REPL Output
Nov 13, 2015
Nov 13, 2015
215
Jun 6, 2016
Jun 6, 2016
216
By default, `repl.REPLServer` instances format output using the
217
[`util.inspect()`][] method before writing the output to the provided Writable
218
stream (`process.stdout` by default). The `useColors` boolean option can be
219
specified at construction to instruct the default writer to use ANSI style
220
codes to colorize the output from the `util.inspect()` method.
Nov 13, 2015
Nov 13, 2015
221
Jun 6, 2016
Jun 6, 2016
222
It is possible to fully customize the output of a `repl.REPLServer` instance
223
by passing a new function in using the `writer` option on construction. The
224
following example, for instance, simply converts any input text to upper case:
Nov 13, 2015
Nov 13, 2015
225
Jun 6, 2016
Jun 6, 2016
226
```js
227
const repl = require('repl');
Nov 13, 2015
Nov 13, 2015
228
Dec 25, 2016
Dec 25, 2016
229
const r = repl.start({prompt: '> ', eval: myEval, writer: myWriter});
Nov 13, 2015
Nov 13, 2015
230
Jun 6, 2016
Jun 6, 2016
231
function myEval(cmd, context, filename, callback) {
Dec 25, 2016
Dec 25, 2016
232
callback(null, cmd);
Jun 6, 2016
Jun 6, 2016
233
}
Nov 13, 2015
Nov 13, 2015
234
Jun 6, 2016
Jun 6, 2016
235
function myWriter(output) {
236
return output.toUpperCase();
237
}
Jan 21, 2016
Jan 21, 2016
238
```
Nov 13, 2015
Nov 13, 2015
239
Nov 17, 2015
Nov 17, 2015
240
## Class: REPLServer
Jun 18, 2016
Jun 18, 2016
241
<!-- YAML
242
added: v0.1.91
243
-->
Nov 17, 2015
Nov 17, 2015
244
Jun 6, 2016
Jun 6, 2016
245
The `repl.REPLServer` class inherits from the [`readline.Interface`][] class.
246
Instances of `repl.REPLServer` are created using the `repl.start()` method and
247
*should not* be created directly using the JavaScript `new` keyword.
Nov 17, 2015
Nov 17, 2015
248
249
### Event: 'exit'
Jun 18, 2016
Jun 18, 2016
250
<!-- YAML
251
added: v0.7.7
252
-->
Nov 17, 2015
Nov 17, 2015
253
Jun 6, 2016
Jun 6, 2016
254
The `'exit'` event is emitted when the REPL is exited either by receiving the
255
`.exit` command as input, the user pressing `<ctrl>-C` twice to signal `SIGINT`,
256
or by pressing `<ctrl>-D` to signal `'end'` on the input stream. The listener
257
callback is invoked without any arguments.
Nov 17, 2015
Nov 17, 2015
258
Jan 21, 2016
Jan 21, 2016
259
```js
260
replServer.on('exit', () => {
Jun 6, 2016
Jun 6, 2016
261
console.log('Received "exit" event from repl!');
Jan 21, 2016
Jan 21, 2016
262
process.exit();
263
});
264
```
Nov 17, 2015
Nov 17, 2015
265
266
### Event: 'reset'
Jun 18, 2016
Jun 18, 2016
267
<!-- YAML
268
added: v0.11.0
269
-->
Nov 17, 2015
Nov 17, 2015
270
Jun 6, 2016
Jun 6, 2016
271
The `'reset'` event is emitted when the REPL's context is reset. This occurs
272
whenever the `.clear` command is received as input *unless* the REPL is using
273
the default evaluator and the `repl.REPLServer` instance was created with the
274
`useGlobal` option set to `true`. The listener callback will be called with a
275
reference to the `context` object as the only argument.
276
277
This can be used primarily to re-initialize REPL context to some pre-defined
278
state as illustrated in the following simple example:
279
280
```js
281
const repl = require('repl');
Nov 17, 2015
Nov 17, 2015
282
Jun 6, 2016
Jun 6, 2016
283
function initializeContext(context) {
284
context.m = 'test';
285
}
Nov 17, 2015
Nov 17, 2015
286
Dec 25, 2016
Dec 25, 2016
287
const r = repl.start({prompt: '> '});
Jun 6, 2016
Jun 6, 2016
288
initializeContext(r.context);
289
290
r.on('reset', initializeContext);
291
```
292
293
When this code is executed, the global `'m'` variable can be modified but then
294
reset to its initial value using the `.clear` command:
Nov 17, 2015
Nov 17, 2015
295
Apr 24, 2017
Apr 24, 2017
296
<!-- eslint-disable -->
Jan 21, 2016
Jan 21, 2016
297
```js
Jun 6, 2016
Jun 6, 2016
298
$ ./node example.js
Dec 25, 2016
Dec 25, 2016
299
> m
Jun 6, 2016
Jun 6, 2016
300
'test'
Dec 25, 2016
Dec 25, 2016
301
> m = 1
Jun 6, 2016
Jun 6, 2016
302
1
Dec 25, 2016
Dec 25, 2016
303
> m
Jun 6, 2016
Jun 6, 2016
304
1
Dec 25, 2016
Dec 25, 2016
305
> .clear
Jun 6, 2016
Jun 6, 2016
306
Clearing context...
Dec 25, 2016
Dec 25, 2016
307
> m
Jun 6, 2016
Jun 6, 2016
308
'test'
309
>
Jan 21, 2016
Jan 21, 2016
310
```
Nov 17, 2015
Nov 17, 2015
311
312
### replServer.defineCommand(keyword, cmd)
Jun 18, 2016
Jun 18, 2016
313
<!-- YAML
314
added: v0.3.0
315
-->
Nov 17, 2015
Nov 17, 2015
316
Mar 2, 2017
Mar 2, 2017
317
* `keyword` {string} The command keyword (*without* a leading `.` character).
Jun 6, 2016
Jun 6, 2016
318
* `cmd` {Object|Function} The function to invoke when the command is processed.
Nov 17, 2015
Nov 17, 2015
319
Jun 6, 2016
Jun 6, 2016
320
The `replServer.defineCommand()` method is used to add new `.`-prefixed commands
321
to the REPL instance. Such commands are invoked by typing a `.` followed by the
322
`keyword`. The `cmd` is either a Function or an object with the following
323
properties:
Nov 17, 2015
Nov 17, 2015
324
Mar 2, 2017
Mar 2, 2017
325
* `help` {string} Help text to be displayed when `.help` is entered (Optional).
Jun 6, 2016
Jun 6, 2016
326
* `action` {Function} The function to execute, optionally accepting a single
327
string argument.
Nov 17, 2015
Nov 17, 2015
328
Jun 6, 2016
Jun 6, 2016
329
The following example shows two new commands added to the REPL instance:
Nov 17, 2015
Nov 17, 2015
330
Jan 21, 2016
Jan 21, 2016
331
```js
332
const repl = require('repl');
Nov 17, 2015
Nov 17, 2015
333
Dec 25, 2016
Dec 25, 2016
334
const replServer = repl.start({prompt: '> '});
Jan 21, 2016
Jan 21, 2016
335
replServer.defineCommand('sayhello', {
336
help: 'Say hello',
Dec 25, 2016
Dec 25, 2016
337
action(name) {
Jun 6, 2016
Jun 6, 2016
338
this.lineParser.reset();
339
this.bufferedCommand = '';
Jun 23, 2016
Jun 23, 2016
340
console.log(`Hello, ${name}!`);
Jan 21, 2016
Jan 21, 2016
341
this.displayPrompt();
342
}
343
});
Dec 25, 2016
Dec 25, 2016
344
replServer.defineCommand('saybye', () => {
Jun 23, 2016
Jun 23, 2016
345
console.log('Goodbye!');
Jun 6, 2016
Jun 6, 2016
346
this.close();
347
});
Jan 21, 2016
Jan 21, 2016
348
```
Nov 17, 2015
Nov 17, 2015
349
Jun 6, 2016
Jun 6, 2016
350
The new commands can then be used from within the REPL instance:
Nov 17, 2015
Nov 17, 2015
351
Jul 14, 2016
Jul 14, 2016
352
```txt
Jan 21, 2016
Jan 21, 2016
353
> .sayhello Node.js User
354
Hello, Node.js User!
Jun 6, 2016
Jun 6, 2016
355
> .saybye
356
Goodbye!
Jan 21, 2016
Jan 21, 2016
357
```
Nov 17, 2015
Nov 17, 2015
358
359
### replServer.displayPrompt([preserveCursor])
Jun 18, 2016
Jun 18, 2016
360
<!-- YAML
361
added: v0.1.91
362
-->
Nov 17, 2015
Nov 17, 2015
363
Mar 2, 2017
Mar 2, 2017
364
* `preserveCursor` {boolean}
Nov 17, 2015
Nov 17, 2015
365
Jun 6, 2016
Jun 6, 2016
366
The `replServer.displayPrompt()` method readies the REPL instance for input
367
from the user, printing the configured `prompt` to a new line in the `output`
368
and resuming the `input` to accept new input.
369
370
When multi-line input is being entered, an ellipsis is printed rather than the
371
'prompt'.
372
373
When `preserveCursor` is `true`, the cursor placement will not be reset to `0`.
374
375
The `replServer.displayPrompt` method is primarily intended to be called from
376
within the action function for commands registered using the
377
`replServer.defineCommand()` method.
Nov 17, 2015
Nov 17, 2015
378
Feb 25, 2016
Feb 25, 2016
379
## repl.start([options])
Jun 18, 2016
Jun 18, 2016
380
<!-- YAML
381
added: v0.1.91
Feb 24, 2017
Feb 24, 2017
382
changes:
383
- version: v5.8.0
384
pr-url: https://github.com/nodejs/node/pull/5388
385
description: The `options` parameter is optional now.
Jun 18, 2016
Jun 18, 2016
386
-->
Oct 28, 2010
Oct 28, 2010
387
Mar 8, 2017
Mar 8, 2017
388
* `options` {Object|string}
Mar 2, 2017
Mar 2, 2017
389
* `prompt` {string} The input prompt to display. Defaults to `> `
Dec 25, 2016
Dec 25, 2016
390
(with a trailing space).
Jun 6, 2016
Jun 6, 2016
391
* `input` {Readable} The Readable stream from which REPL input will be read.
392
Defaults to `process.stdin`.
393
* `output` {Writable} The Writable stream to which REPL output will be
394
written. Defaults to `process.stdout`.
395
* `terminal` {boolean} If `true`, specifies that the `output` should be
396
treated as a a TTY terminal, and have ANSI/VT100 escape codes written to it.
397
Defaults to checking the value of the `isTTY` property on the `output`
398
stream upon instantiation.
399
* `eval` {Function} The function to be used when evaluating each given line
400
of input. Defaults to an async wrapper for the JavaScript `eval()`
401
function. An `eval` function can error with `repl.Recoverable` to indicate
402
the input was incomplete and prompt for additional lines.
403
* `useColors` {boolean} If `true`, specifies that the default `writer`
404
function should include ANSI color styling to REPL output. If a custom
405
`writer` function is provided then this has no effect. Defaults to the
406
REPL instances `terminal` value.
407
* `useGlobal` {boolean} If `true`, specifies that the default evaluation
408
function will use the JavaScript `global` as the context as opposed to
409
creating a new separate context for the REPL instance. Defaults to `false`.
410
* `ignoreUndefined` {boolean} If `true`, specifies that the default writer
411
will not output the return value of a command if it evaluates to
412
`undefined`. Defaults to `false`.
413
* `writer` {Function} The function to invoke to format the output of each
414
command before writing to `output`. Defaults to [`util.inspect()`][].
Jul 8, 2016
Jul 8, 2016
415
* `completer` {Function} An optional function used for custom Tab auto
416
completion. See [`readline.InterfaceCompleter`][] for an example.
Mar 7, 2017
Mar 7, 2017
417
* `replMode` {symbol} A flag that specifies whether the default evaluator
418
executes all JavaScript commands in strict mode or default (sloppy) mode.
419
Acceptable values are:
Jun 6, 2016
Jun 6, 2016
420
* `repl.REPL_MODE_SLOPPY` - evaluates expressions in sloppy mode.
421
* `repl.REPL_MODE_STRICT` - evaluates expressions in strict mode. This is
422
equivalent to prefacing every repl statement with `'use strict'`.
Mar 7, 2017
Mar 7, 2017
423
* `repl.REPL_MODE_MAGIC` - This value is **deprecated**, since enhanced
424
spec compliance in V8 has rendered magic mode unnecessary. It is now
425
equivalent to `repl.REPL_MODE_SLOPPY` (documented above).
Jul 28, 2016
Jul 28, 2016
426
* `breakEvalOnSigint` - Stop evaluating the current piece of code when
427
`SIGINT` is received, i.e. `Ctrl+C` is pressed. This cannot be used together
428
with a custom `eval` function. Defaults to `false`.
Jun 6, 2016
Jun 6, 2016
429
430
The `repl.start()` method creates and starts a `repl.REPLServer` instance.
431
Dec 12, 2016
Dec 12, 2016
432
If `options` is a string, then it specifies the input prompt:
433
434
```js
435
const repl = require('repl');
436
437
// a Unix style prompt
438
repl.start('$ ');
439
```
440
Jun 6, 2016
Jun 6, 2016
441
## The Node.js REPL
442
443
Node.js itself uses the `repl` module to provide its own interactive interface
Sep 30, 2016
Sep 30, 2016
444
for executing JavaScript. This can be used by executing the Node.js binary
445
without passing any arguments (or by passing the `-i` argument):
Oct 11, 2011
Oct 11, 2011
446
Apr 24, 2017
Apr 24, 2017
447
<!-- eslint-disable -->
Jun 6, 2016
Jun 6, 2016
448
```js
449
$ node
Dec 25, 2016
Dec 25, 2016
450
> const a = [1, 2, 3];
Jun 6, 2016
Jun 6, 2016
451
[ 1, 2, 3 ]
452
> a.forEach((v) => {
453
... console.log(v);
454
... });
455
1
456
2
457
3
458
```
Oct 21, 2011
Oct 21, 2011
459
Jun 6, 2016
Jun 6, 2016
460
### Environment Variable Options
Mar 26, 2012
Mar 26, 2012
461
Jun 6, 2016
Jun 6, 2016
462
Various behaviors of the Node.js REPL can be customized using the following
463
environment variables:
Mar 26, 2012
Mar 26, 2012
464
Jun 6, 2016
Jun 6, 2016
465
- `NODE_REPL_HISTORY` - When a valid path is given, persistent REPL history
466
will be saved to the specified file rather than `.node_repl_history` in the
467
user's home directory. Setting this value to `""` will disable persistent
468
REPL history. Whitespace will be trimmed from the value.
469
- `NODE_REPL_HISTORY_SIZE` - Defaults to `1000`. Controls how many lines of
470
history will be persisted if history is available. Must be a positive number.
471
- `NODE_REPL_MODE` - May be any of `sloppy`, `strict`, or `magic`. Defaults
Mar 7, 2017
Mar 7, 2017
472
to `sloppy`, which will allow non-strict mode code to be run. `magic` is
473
**deprecated** and treated as an alias of `sloppy`.
Mar 26, 2012
Mar 26, 2012
474
Jun 6, 2016
Jun 6, 2016
475
### Persistent History
Mar 26, 2012
Mar 26, 2012
476
Jun 6, 2016
Jun 6, 2016
477
By default, the Node.js REPL will persist history between `node` REPL sessions
478
by saving inputs to a `.node_repl_history` file located in the user's home
479
directory. This can be disabled by setting the environment variable
480
`NODE_REPL_HISTORY=""`.
Mar 28, 2012
Mar 28, 2012
481
Jun 6, 2016
Jun 6, 2016
482
#### NODE_REPL_HISTORY_FILE
Jun 18, 2016
Jun 18, 2016
483
<!-- YAML
484
added: v2.0.0
485
deprecated: v3.0.0
486
-->
Mar 26, 2012
Mar 26, 2012
487
Aug 4, 2016
Aug 4, 2016
488
> Stability: 0 - Deprecated: Use `NODE_REPL_HISTORY` instead.
Nov 2, 2011
Nov 2, 2011
489
Jun 6, 2016
Jun 6, 2016
490
Previously in Node.js/io.js v2.x, REPL history was controlled by using a
491
`NODE_REPL_HISTORY_FILE` environment variable, and the history was saved in JSON
492
format. This variable has now been deprecated, and the old JSON REPL history
493
file will be automatically converted to a simplified plain text format. This new
494
file will be saved to either the user's home directory, or a directory defined
495
by the `NODE_REPL_HISTORY` variable, as documented in the
496
[Environment Variable Options](#repl_environment_variable_options).
Mar 28, 2012
Mar 28, 2012
497
Jun 6, 2016
Jun 6, 2016
498
### Using the Node.js REPL with advanced line-editors
May 1, 2015
May 1, 2015
499
Jun 6, 2016
Jun 6, 2016
500
For advanced line-editors, start Node.js with the environmental variable
501
`NODE_NO_READLINE=1`. This will start the main and debugger REPL in canonical
502
terminal settings which will allow you to use with `rlwrap`.
Oct 11, 2011
Oct 11, 2011
503
Jun 6, 2016
Jun 6, 2016
504
For example, you could add this to your bashrc file:
May 12, 2016
May 12, 2016
505
Jun 6, 2016
Jun 6, 2016
506
```text
507
alias node="env NODE_NO_READLINE=1 rlwrap node"
May 12, 2016
May 12, 2016
508
```
Oct 28, 2010
Oct 28, 2010
509
Jun 6, 2016
Jun 6, 2016
510
### Starting multiple REPL instances against a single running instance
May 17, 2015
May 17, 2015
511
Jun 6, 2016
Jun 6, 2016
512
It is possible to create and run multiple REPL instances against a single
513
running instance of Node.js that share a single `global` object but have
514
separate I/O interfaces.
Oct 28, 2010
Oct 28, 2010
515
Jun 6, 2016
Jun 6, 2016
516
The following example, for instance, provides separate REPLs on `stdin`, a Unix
517
socket, and a TCP socket:
Oct 28, 2010
Oct 28, 2010
518
Jan 21, 2016
Jan 21, 2016
519
```js
520
const net = require('net');
521
const repl = require('repl');
Dec 25, 2016
Dec 25, 2016
522
let connections = 0;
Jan 21, 2016
Jan 21, 2016
523
524
repl.start({
525
prompt: 'Node.js via stdin> ',
526
input: process.stdin,
527
output: process.stdout
528
});
529
530
net.createServer((socket) => {
531
connections += 1;
532
repl.start({
533
prompt: 'Node.js via Unix socket> ',
534
input: socket,
535
output: socket
536
}).on('exit', () => {
537
socket.end();
Jul 15, 2016
Jul 15, 2016
538
});
Jan 21, 2016
Jan 21, 2016
539
}).listen('/tmp/node-repl-sock');
540
541
net.createServer((socket) => {
542
connections += 1;
543
repl.start({
544
prompt: 'Node.js via TCP socket> ',
545
input: socket,
546
output: socket
547
}).on('exit', () => {
548
socket.end();
549
});
550
}).listen(5001);
551
```
Oct 28, 2010
Oct 28, 2010
552
Jun 6, 2016
Jun 6, 2016
553
Running this application from the command line will start a REPL on stdin.
554
Other REPL clients may connect through the Unix socket or TCP socket. `telnet`,
555
for instance, is useful for connecting to TCP sockets, while `socat` can be used
556
to connect to both Unix and TCP sockets.
Oct 28, 2010
Oct 28, 2010
557
Jun 6, 2016
Jun 6, 2016
558
By starting a REPL from a Unix socket-based server instead of stdin, it is
559
possible to connect to a long-running Node.js process without restarting it.
Oct 28, 2010
Oct 28, 2010
560
Mar 26, 2012
Mar 26, 2012
561
For an example of running a "full-featured" (`terminal`) REPL over
562
a `net.Server` and `net.Socket` instance, see: https://gist.github.com/2209310
563
Dec 25, 2016
Dec 25, 2016
564
For an example of running a REPL instance over [curl(1)][],
Mar 26, 2012
Mar 26, 2012
565
see: https://gist.github.com/2053342
566
Jun 6, 2016
Jun 6, 2016
567
[stream]: stream.html
Dec 3, 2015
Dec 3, 2015
568
[`util.inspect()`]: util.html#util_util_inspect_object_options
Jun 6, 2016
Jun 6, 2016
569
[`readline.Interface`]: readline.html#readline_class_interface
Jul 8, 2016
Jul 8, 2016
570
[`readline.InterfaceCompleter`]: readline.html#readline_use_of_the_completer_function
Dec 25, 2016
Dec 25, 2016
571
[curl(1)]: https://curl.haxx.se/docs/manpage.html