How to really take advantage of the NodeJS REPL

Happily debug your project

Find out how to use your asynchronous methods in the NodeJS REPL console.


Having Ruby on Rails and NodeJS as our two main backend technologies have both benefits and drawbacks. It’s really great to switch up the everyday language and learn from another technology, but of course, you also begin missing some features from the one you left behind.

This is what happened to rails: they miss their console (among other things). The rails console is the place where all the app methods and objects are exposed for interaction. The whole app state can be watched and manipulated with it, which makes production bugs easier to track.

As a (mainly) NodeJS developer, I wanted to find a similar way to manage state and to see the persistence models without entering the database. So I started by investigating the Node REPL.

Starting with REPL

The NodeJS REPL Console was the starting point. The big difference between this and the Rails console is that the rails console loads all your gems, classes, and other objects automatically, whereas here we have to do it manually.

The first thing we needed to decide was which objects and functions we wanted to expose. We decided only to expose our services and the ORM object, but this is open to discussion: if you have any suggestions or ideas on what to expose and how, please let me know in the comments.

The first approach is to require a service file when we start the console:

So, let’s start by creating a `console.js`, starting the REPL and adding our objects:

We can run it with `node console.js`

Here we are requiring every file in the service and adding it to our REPL context as filename + “Service”.

Note that as we are using an MVC model, the services are the ones responsible for interacting not only with the database but with any external service. Integrations are by far the most complicated thing to automatically test, therefore, we are trying to find a solution for this by exposing them in the console. Interact and see the results!

The problem

In our express js bootstrap, we have an example MVC app with users and books. We are going to try to use our user’s service to get a user from the database.

We can see that the query was executed but no result was shown. Our services methods return Promises, and for us to see the result, we will have to resolve that promise.

So, we are we going to have to do this every time we want to check a service method? Clearly, there is a better solution for this: using Async/Await.

The solution

We are going to convert our service methods to make them look “synchronic”. Declare an async function, await the result, return it and done. The code now looks like this:

Let’s try it!


Now we have a way to check our project flows with an interactive terminal. We can reproduce our bugs without hitting endpoints, test smaller functionalities and abstract from the database. Let the debugging begin!

Please do not hesitate to contact me for any feedback, suggestion or question you may have.