-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
doc: add esm examples to node:repl
#55432
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,11 @@ The `node:repl` module provides a Read-Eval-Print-Loop (REPL) implementation | |
that is available both as a standalone program or includible in other | ||
applications. It can be accessed using: | ||
|
||
```js | ||
```mjs | ||
import repl from 'node:repl'; | ||
``` | ||
|
||
```cjs | ||
const repl = require('node:repl'); | ||
``` | ||
|
||
|
@@ -106,7 +110,14 @@ The default evaluator provides access to any variables that exist in the global | |
scope. It is possible to expose a variable to the REPL explicitly by assigning | ||
it to the `context` object associated with each `REPLServer`: | ||
|
||
```js | ||
```mjs | ||
import repl from 'node:repl'; | ||
const msg = 'message'; | ||
|
||
repl.start('> ').context.m = msg; | ||
``` | ||
|
||
```cjs | ||
const repl = require('node:repl'); | ||
const msg = 'message'; | ||
|
||
|
@@ -124,7 +135,19 @@ $ node repl_test.js | |
Context properties are not read-only by default. To specify read-only globals, | ||
context properties must be defined using `Object.defineProperty()`: | ||
|
||
```js | ||
```mjs | ||
import repl from 'node:repl'; | ||
const msg = 'message'; | ||
|
||
const r = repl.start('> '); | ||
Object.defineProperty(r.context, 'm', { | ||
configurable: false, | ||
enumerable: true, | ||
value: msg, | ||
}); | ||
``` | ||
|
||
```cjs | ||
const repl = require('node:repl'); | ||
const msg = 'message'; | ||
|
||
|
@@ -280,20 +303,34 @@ When a new [`repl.REPLServer`][] is created, a custom evaluation function may be | |
provided. This can be used, for instance, to implement fully customized REPL | ||
applications. | ||
|
||
The following illustrates a hypothetical example of a REPL that performs | ||
translation of text from one language to another: | ||
The following illustrates an example of a REPL that squares a given number: | ||
|
||
```js | ||
```mjs | ||
import repl from 'node:repl'; | ||
|
||
function byThePowerOfTwo(number) { | ||
return number * number; | ||
} | ||
|
||
function myEval(cmd, context, filename, callback) { | ||
callback(null, byThePowerOfTwo(cmd)); | ||
} | ||
|
||
repl.start({ prompt: 'Enter a number: ', eval: myEval }); | ||
``` | ||
|
||
```cjs | ||
const repl = require('node:repl'); | ||
const { Translator } = require('translator'); | ||
|
||
const myTranslator = new Translator('en', 'fr'); | ||
function byThePowerOfTwo(number) { | ||
return number * number; | ||
} | ||
|
||
function myEval(cmd, context, filename, callback) { | ||
callback(null, myTranslator.translate(cmd)); | ||
callback(null, byThePowerOfTwo(cmd)); | ||
} | ||
|
||
repl.start({ prompt: '> ', eval: myEval }); | ||
repl.start({ prompt: 'Enter a number: ', eval: myEval }); | ||
``` | ||
|
||
#### Recoverable errors | ||
|
@@ -354,7 +391,21 @@ To fully customize the output of a [`repl.REPLServer`][] instance pass in a new | |
function for the `writer` option on construction. The following example, for | ||
instance, simply converts any input text to upper case: | ||
|
||
```js | ||
```mjs | ||
import repl from 'node:repl'; | ||
|
||
const r = repl.start({ prompt: '> ', eval: myEval, writer: myWriter }); | ||
|
||
function myEval(cmd, context, filename, callback) { | ||
callback(null, cmd); | ||
} | ||
|
||
function myWriter(output) { | ||
return output.toUpperCase(); | ||
} | ||
``` | ||
|
||
```cjs | ||
const repl = require('node:repl'); | ||
|
||
const r = repl.start({ prompt: '> ', eval: myEval, writer: myWriter }); | ||
|
@@ -380,7 +431,16 @@ added: v0.1.91 | |
Instances of `repl.REPLServer` are created using the [`repl.start()`][] method | ||
or directly using the JavaScript `new` keyword. | ||
|
||
```js | ||
```mjs | ||
import repl from 'node:repl'; | ||
|
||
const options = { useColors: true }; | ||
|
||
const firstInstance = repl.start(options); | ||
const secondInstance = new repl.REPLServer(options); | ||
``` | ||
|
||
```cjs | ||
const repl = require('node:repl'); | ||
|
||
const options = { useColors: true }; | ||
|
@@ -424,7 +484,20 @@ reference to the `context` object as the only argument. | |
This can be used primarily to re-initialize REPL context to some pre-defined | ||
state: | ||
|
||
```js | ||
```mjs | ||
import repl from 'node:repl'; | ||
|
||
function initializeContext(context) { | ||
context.m = 'test'; | ||
} | ||
|
||
const r = repl.start({ prompt: '> ' }); | ||
initializeContext(r.context); | ||
|
||
r.on('reset', initializeContext); | ||
``` | ||
|
||
```cjs | ||
const repl = require('node:repl'); | ||
|
||
function initializeContext(context) { | ||
|
@@ -475,7 +548,25 @@ properties: | |
|
||
The following example shows two new commands added to the REPL instance: | ||
|
||
```js | ||
```mjs | ||
import repl from 'node:repl'; | ||
|
||
const replServer = repl.start({ prompt: '> ' }); | ||
replServer.defineCommand('sayhello', { | ||
help: 'Say hello', | ||
action(name) { | ||
this.clearBufferedCommand(); | ||
console.log(`Hello, ${name}!`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this output doesn't quite make sense - where would There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's passed when you execute the > .sayhello bnb
Hello, bnb! |
||
this.displayPrompt(); | ||
}, | ||
}); | ||
replServer.defineCommand('saybye', function saybye() { | ||
console.log('Goodbye!'); | ||
this.close(); | ||
}); | ||
``` | ||
|
||
```cjs | ||
const repl = require('node:repl'); | ||
|
||
const replServer = repl.start({ prompt: '> ' }); | ||
|
@@ -637,7 +728,14 @@ The `repl.start()` method creates and starts a [`repl.REPLServer`][] instance. | |
|
||
If `options` is a string, then it specifies the input prompt: | ||
|
||
```js | ||
```mjs | ||
import repl from 'node:repl'; | ||
|
||
// a Unix style prompt | ||
repl.start('$ '); | ||
``` | ||
|
||
```cjs | ||
const repl = require('node:repl'); | ||
|
||
// a Unix style prompt | ||
|
@@ -709,7 +807,43 @@ separate I/O interfaces. | |
The following example, for instance, provides separate REPLs on `stdin`, a Unix | ||
socket, and a TCP socket: | ||
|
||
```js | ||
```mjs | ||
import net from 'node:net'; | ||
import repl from 'node:repl'; | ||
import process from 'node:process'; | ||
|
||
let 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); | ||
``` | ||
|
||
```cjs | ||
const net = require('node:net'); | ||
const repl = require('node:repl'); | ||
let connections = 0; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this one is working as it's described? Maybe I'm misunderstanding what's supposed to happen here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It resets the context,
m
equals to'test'
, but if you were to do something likem = 'other thing'
and then you call.clear
it will reset the context, so when you print the value ofm
it would be reset back to'test'
😊full example: