TIL About Node.js’ REPL Module

petecorey

Pete Corey

Posted on September 13, 2019

TIL About Node.js’ REPL Module

Today I learned that Node.js ships with a repl module that can be used to spin up a full-featured REPL on any Node.js process. This can be a fantastic tool for debugging a running server, or manually triggering back-end events.

Let’s assume that we’ve built a Node.js server who’s entry point is a server.js file. Let’s also assume that we have a constant (maybe pulled from our environment, maybe elsewhere) called REPL who’s truthiness determines whether we should start our REPL instance on standard in. Spinning up our REPL is as easy as:

if (REPL) {
    require('repl').start();
}

Once our server starts up, we’ll be greeted by a familiar prompt:

Starting server...
Listening on localhost:8080!
>

Fantastic! Normal REPL rules apply. Our server will continue to run and its output will continue to stream to standard out. Our REPL prompt will stick to the bottom of the tail, as expected.

More advanced options can be gleaned from the repl documentation. Happy REPLing!

💖 💪 🙅 🚩
petecorey
Pete Corey

Posted on September 13, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related