Skip to main content
REPLServer.prototype.defineCommand - repl - Node documentation
method REPLServer.prototype.defineCommand

Usage in Deno

import { REPLServer } from "node:repl";
REPLServer.prototype.defineCommand(
keyword: string,
): 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 the keyword. 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:

import repl from '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!

Parameters

keyword: string

The command keyword (without a leading . character).

The function to invoke when the command is processed.

Return Type

void