API with NestJS #68. Interacting with the application through REPL


This entry is part 68 of 156 in the API with NestJS

The NestJS team officially announced NestJS 9 on July 8th. One of its features is Read-Eval-Print-Loop (REPL) implementation. It is an environment that allows us to interact with our NestJS application through the terminal.

In this article, we go through how to set up REPL with NestJS. We also use features built into NodeJS to make the most out of REPL in a way that the official NestJS documentation does not show currently.

Source: trilon.io

Setting up REPL

For REPL to work, we should create a file in the same directory as .


Let’s add a new script to our file to run it.


To start using REPL, we need to run .Functions built into NestJS REPL

NestJS gives us a set of functions we can use to interact with our application.

To list all of built-in features, we can call the  function.


We can use the utility if we want to list the public methods of a particular injectable or controller.


With the function, we can get an instance of an injectable. A good example is getting a service and calling its method.

In REPL, the keyword is supported at the top level.

The crucial thing to realize is that when we use NestJS REPL, we run an instance of Node.js. It means that we can declare variables, for example.

Unfortunately, we can’t type multiple lines before clicking enter, so we must input each line separately. To deal with the above issue, we can type and click enter to go into the editor mode.

Instead of we can also call with the same result.

Trying to define the variable more than once results in an error.

Uncaught SyntaxError: Identifier ‘user’ has already been declared

The utility built into REPL resets the whole context and the declared variables. Unfortunately, this also gets rid of the features provided by NestJS, so we should abstain from using the utility.


With the functionality, we can see all registered modules with their controllers and providers.

By typing and clicking enter, we can see the documentation of the feature.

Print all registered modules as a list together with their controllers and providers.
If the argument is passed in, for example, “debug(MyModule)” then it will only print components of this specific module.

In the documentation, we can see that we can pass a particular module to print only its components.


Allows us to pull out a specific instance from a selected module and navigate the modules tree. It uses the method of the class that you can find in the repository.


The functionality resolves transient and request-scoped providers. It works the same as the method described in the documentation.

In our application, we have the PostsLoaders class. Since we marked it as request-scoped, we need to use instead of the function to refer to it.

Using the underscore variable

To simplify how we use REPL, we can take advantage of the variable. It contains the result of the previously evaluated expression.

If we explicitly assign a value to the variable, the disable the above behaviour.

Besides , we can also use to access the last error.

Adding custom functions to the context

If we press the tab button twice, we see the full context of our REPL session.

We can see that NestJS loads our injectables into the context, such as the or . Besides that, it also injects the features such as , and . Fortunately, we have access to the context in the file.

We can take advantage of the above and define the functions we often use.


The variable is an instance of the REPLServer class.

Thanks to the fact that we’ve added the function to the context, we can now use it directly with our NestJS REPL.

Saving and loading REPL sessions

Let’s say we’ve been playing with REPL for quite some time. We might decide that we want to store all of the commands we’ve run so far. We can achieve that with the command.

Doing the above stores all of the commands we’ve run to the specified file.


Thanks to saving the current session, we can use the command to run all of the commands specified in the file.


In this article, we’ve gone through the recent REPL feature that NestJS introduced. We’ve learned how to use it to interact with our application, for example, by calling various methods on our services. For example, it comes in handy when testing specific parts of our application. We’ve also gone through various features of REPL built into Node.js and learned how to utilize them to increase the developer experience. For example, we’ve managed to write multi-line scripts and attach custom functions to the NestJS REPL sessions. All of the above can be useful and make our life way easier.

Series Navigation<< API with NestJS #67. Migrating to TypeORM 0.3API with NestJS #69. Database migrations with TypeORM >>
Notify of
Inline Feedbacks
View all comments