@@ -10,7 +10,11 @@ The `node:repl` module provides a Read-Eval-Print-Loop (REPL) implementation
10
10
that is available both as a standalone program or includible in other
11
11
applications. It can be accessed using:
12
12
13
- ``` js
13
+ ``` mjs
14
+ import repl from ' node:repl' ;
15
+ ```
16
+
17
+ ``` cjs
14
18
const repl = require (' node:repl' );
15
19
```
16
20
@@ -106,7 +110,14 @@ The default evaluator provides access to any variables that exist in the global
106
110
scope. It is possible to expose a variable to the REPL explicitly by assigning
107
111
it to the ` context ` object associated with each ` REPLServer ` :
108
112
109
- ``` js
113
+ ``` mjs
114
+ import repl from ' node:repl' ;
115
+ const msg = ' message' ;
116
+
117
+ repl .start (' > ' ).context .m = msg;
118
+ ```
119
+
120
+ ``` cjs
110
121
const repl = require (' node:repl' );
111
122
const msg = ' message' ;
112
123
@@ -124,7 +135,19 @@ $ node repl_test.js
124
135
Context properties are not read-only by default. To specify read-only globals,
125
136
context properties must be defined using ` Object.defineProperty() ` :
126
137
127
- ``` js
138
+ ``` mjs
139
+ import repl from ' node:repl' ;
140
+ const msg = ' message' ;
141
+
142
+ const r = repl .start (' > ' );
143
+ Object .defineProperty (r .context , ' m' , {
144
+ configurable: false ,
145
+ enumerable: true ,
146
+ value: msg,
147
+ });
148
+ ```
149
+
150
+ ``` cjs
128
151
const repl = require (' node:repl' );
129
152
const msg = ' message' ;
130
153
@@ -280,20 +303,34 @@ When a new [`repl.REPLServer`][] is created, a custom evaluation function may be
280
303
provided. This can be used, for instance, to implement fully customized REPL
281
304
applications.
282
305
283
- The following illustrates a hypothetical example of a REPL that performs
284
- translation of text from one language to another:
306
+ The following illustrates an example of a REPL that squares a given number:
285
307
286
- ``` js
308
+ ``` mjs
309
+ import repl from ' node:repl' ;
310
+
311
+ function byThePowerOfTwo (number ) {
312
+ return number * number;
313
+ }
314
+
315
+ function myEval (cmd , context , filename , callback ) {
316
+ callback (null , byThePowerOfTwo (cmd));
317
+ }
318
+
319
+ repl .start ({ prompt: ' Enter a number: ' , eval: myEval });
320
+ ```
321
+
322
+ ``` cjs
287
323
const repl = require (' node:repl' );
288
- const { Translator } = require (' translator' );
289
324
290
- const myTranslator = new Translator (' en' , ' fr' );
325
+ function byThePowerOfTwo (number ) {
326
+ return number * number;
327
+ }
291
328
292
329
function myEval (cmd , context , filename , callback ) {
293
- callback (null , myTranslator . translate (cmd));
330
+ callback (null , byThePowerOfTwo (cmd));
294
331
}
295
332
296
- repl .start ({ prompt: ' > ' , eval: myEval });
333
+ repl .start ({ prompt: ' Enter a number: ' , eval: myEval });
297
334
```
298
335
299
336
#### Recoverable errors
@@ -354,7 +391,21 @@ To fully customize the output of a [`repl.REPLServer`][] instance pass in a new
354
391
function for the ` writer ` option on construction. The following example, for
355
392
instance, simply converts any input text to upper case:
356
393
357
- ``` js
394
+ ``` mjs
395
+ import repl from ' node:repl' ;
396
+
397
+ const r = repl .start ({ prompt: ' > ' , eval: myEval, writer: myWriter });
398
+
399
+ function myEval (cmd , context , filename , callback ) {
400
+ callback (null , cmd);
401
+ }
402
+
403
+ function myWriter (output ) {
404
+ return output .toUpperCase ();
405
+ }
406
+ ```
407
+
408
+ ``` cjs
358
409
const repl = require (' node:repl' );
359
410
360
411
const r = repl .start ({ prompt: ' > ' , eval: myEval, writer: myWriter });
@@ -380,7 +431,16 @@ added: v0.1.91
380
431
Instances of ` repl.REPLServer ` are created using the [ ` repl.start() ` ] [ ] method
381
432
or directly using the JavaScript ` new ` keyword.
382
433
383
- ``` js
434
+ ``` mjs
435
+ import repl from ' node:repl' ;
436
+
437
+ const options = { useColors: true };
438
+
439
+ const firstInstance = repl .start (options);
440
+ const secondInstance = new repl.REPLServer (options);
441
+ ```
442
+
443
+ ``` cjs
384
444
const repl = require (' node:repl' );
385
445
386
446
const options = { useColors: true };
@@ -424,7 +484,20 @@ reference to the `context` object as the only argument.
424
484
This can be used primarily to re-initialize REPL context to some pre-defined
425
485
state:
426
486
427
- ``` js
487
+ ``` mjs
488
+ import repl from ' node:repl' ;
489
+
490
+ function initializeContext (context ) {
491
+ context .m = ' test' ;
492
+ }
493
+
494
+ const r = repl .start ({ prompt: ' > ' });
495
+ initializeContext (r .context );
496
+
497
+ r .on (' reset' , initializeContext);
498
+ ```
499
+
500
+ ``` cjs
428
501
const repl = require (' node:repl' );
429
502
430
503
function initializeContext (context ) {
@@ -475,7 +548,25 @@ properties:
475
548
476
549
The following example shows two new commands added to the REPL instance:
477
550
478
- ``` js
551
+ ``` mjs
552
+ import repl from ' node:repl' ;
553
+
554
+ const replServer = repl .start ({ prompt: ' > ' });
555
+ replServer .defineCommand (' sayhello' , {
556
+ help: ' Say hello' ,
557
+ action (name ) {
558
+ this .clearBufferedCommand ();
559
+ console .log (` Hello, ${ name} !` );
560
+ this .displayPrompt ();
561
+ },
562
+ });
563
+ replServer .defineCommand (' saybye' , function saybye () {
564
+ console .log (' Goodbye!' );
565
+ this .close ();
566
+ });
567
+ ```
568
+
569
+ ``` cjs
479
570
const repl = require (' node:repl' );
480
571
481
572
const replServer = repl .start ({ prompt: ' > ' });
@@ -637,7 +728,14 @@ The `repl.start()` method creates and starts a [`repl.REPLServer`][] instance.
637
728
638
729
If ` options ` is a string, then it specifies the input prompt:
639
730
640
- ``` js
731
+ ``` mjs
732
+ import repl from ' node:repl' ;
733
+
734
+ // a Unix style prompt
735
+ repl .start (' $ ' );
736
+ ```
737
+
738
+ ``` cjs
641
739
const repl = require (' node:repl' );
642
740
643
741
// a Unix style prompt
@@ -709,7 +807,43 @@ separate I/O interfaces.
709
807
The following example, for instance, provides separate REPLs on ` stdin ` , a Unix
710
808
socket, and a TCP socket:
711
809
712
- ``` js
810
+ ``` mjs
811
+ import net from ' node:net' ;
812
+ import repl from ' node:repl' ;
813
+ import process from ' node:process' ;
814
+
815
+ let connections = 0 ;
816
+
817
+ repl .start ({
818
+ prompt: ' Node.js via stdin> ' ,
819
+ input: process .stdin ,
820
+ output: process .stdout ,
821
+ });
822
+
823
+ net .createServer ((socket ) => {
824
+ connections += 1 ;
825
+ repl .start ({
826
+ prompt: ' Node.js via Unix socket> ' ,
827
+ input: socket,
828
+ output: socket,
829
+ }).on (' exit' , () => {
830
+ socket .end ();
831
+ });
832
+ }).listen (' /tmp/node-repl-sock' );
833
+
834
+ net .createServer ((socket ) => {
835
+ connections += 1 ;
836
+ repl .start ({
837
+ prompt: ' Node.js via TCP socket> ' ,
838
+ input: socket,
839
+ output: socket,
840
+ }).on (' exit' , () => {
841
+ socket .end ();
842
+ });
843
+ }).listen (5001 );
844
+ ```
845
+
846
+ ``` cjs
713
847
const net = require (' node:net' );
714
848
const repl = require (' node:repl' );
715
849
let connections = 0 ;
0 commit comments