> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/nodejs/userland-migrations/llms.txt
> Use this file to discover all available pages before exploring further.

# REPL Classes with new Keyword

> Add new keyword when instantiating REPL classes like REPLServer and Recoverable (DEP0185)

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](https://nodejs.org/api/deprecations.html#DEP0185) for more details.

## Usage

Run this codemod with:

```bash theme={null}
npx codemod nodejs/repl-classes-with-new
```

## Before/After

```diff theme={null}
  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

```diff theme={null}
  const repl = require('node:repl');

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

### Handling Recoverable Errors

```diff theme={null}
  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);
      }
    }
  }
```

<Tip>
  Modern JavaScript requires all classes to be instantiated with the `new` keyword. This deprecation aligns Node.js REPL classes with standard JavaScript class syntax.
</Tip>
