method REPLServer.prototype.defineCommand
Usage in Deno
import { REPLServer } from "node:repl";
REPLServer.prototype.defineCommand(keyword: string,cmd: REPLCommandAction | REPLCommand,): void
The replServer.defineCommand()
method is used to add new .
-prefixed commands
to the REPL instance. Such commands are invoked by typing a .
followed by thekeyword
. The cmd
is either a Function
or an Object
with the following
properties:
The following example shows two new commands added to the REPL instance:
const repl = require('node:repl'); const replServer = repl.start({ prompt: '> ' }); replServer.defineCommand('sayhello', { help: 'Say hello', action(name) { this.clearBufferedCommand(); console.log(`Hello, ${name}!`); this.displayPrompt(); }, }); replServer.defineCommand('saybye', function saybye() { console.log('Goodbye!'); this.close(); });
The new commands can then be used from within the REPL instance:
> .sayhello Node.js User Hello, Node.js User! > .saybye Goodbye!
cmd: REPLCommandAction | REPLCommand
The function to invoke when the command is processed.
void