Skip to main content
This recipe ensures REPL classes like REPLServer and Recoverable are properly instantiated with the new keyword.

Deprecation

Node.js deprecated calling REPL constructors without the new keyword. Classes must be instantiated with new. See DEP0185 for more details.

Usage

Run this codemod with:
npx codemod nodejs/repl-classes-with-new

Before/After

  const repl = require("node:repl");
  const { REPLServer, Recoverable } = require("node:repl");
  import { REPLServer } from "node:repl";
  const { REPLServer: REPL } = await import("node:repl");

  // Missing 'new' keyword
- const server1 = repl.REPLServer();
+ // With 'new' keyword
+ const server1 = new repl.REPLServer();
- const server2 = REPLServer({ prompt: ">>> " });
+ const server2 = new REPLServer({ prompt: ">>> " });
- const server3 = repl.Recoverable();
+ const server3 = new repl.Recoverable();
- const error = Recoverable(new SyntaxError());
+ const error = new Recoverable(new SyntaxError());
- const server4 = REPL({ prompt: ">>> " });
+ const server4 = new REPL({ prompt: ">>> " });

What It Does

  • Adds new keyword before REPLServer() and Recoverable() calls
  • Works with both direct imports and namespace imports (e.g., repl.REPLServer())
  • Handles CommonJS and ESM module patterns
  • Supports aliased imports (e.g., REPLServer as REPL)

Affected Classes

The following REPL classes are updated:
  • REPLServer - The main REPL server class
  • Recoverable - Error class for recoverable syntax errors

Example Scenarios

Creating a Custom REPL

  const repl = require('node:repl');

- const server = repl.REPLServer({
+ const server = new repl.REPLServer({
    prompt: 'my-repl> ',
    eval: customEval,
  });

Handling Recoverable Errors

  const { Recoverable } = require('node:repl');

  function customEval(cmd, context, filename, callback) {
    try {
      const result = evaluate(cmd);
      callback(null, result);
    } catch (err) {
      if (isRecoverable(err)) {
-       callback(Recoverable(err));
+       callback(new Recoverable(err));
      } else {
        callback(err);
      }
    }
  }
Modern JavaScript requires all classes to be instantiated with the new keyword. This deprecation aligns Node.js REPL classes with standard JavaScript class syntax.

Build docs developers (and LLMs) love