[Nest.js] Nest.js Read-Eval-Print-Loop (REPL) environment setup quick notes

Photo by v2osk on Unsplash

[Nest.js] Nest.js Read-Eval-Print-Loop (REPL) environment setup quick notes

Official doc is already a good place to get started with https://docs.nestjs.com/recipes/repl

REPL is a simple interactive environment that takes single user inputs, executes them, and returns the result to the user. The REPL feature lets you inspect your dependency graph and call methods on your providers (and controllers) directly from your terminal.

For my own specific setup I have

src/repl.ts

/* eslint-disable no-console */
import { repl } from '@nestjs/core';

import AppModule from './app.module';

async function bootstrap() {
  const replServer = await repl(AppModule);
  replServer.setupHistory('.nestjs_repl_history', err => {
    if (err) {
      console.error(err);
    }
  });
}
bootstrap();

And I didn't touch my docker compose. After running nest.js in docker, I just use Docker Desktop's Exec tab (or we can run docker exec shell) to start the REPL console. Note that watch mode is turned off: with it turned on the performance is very laggy.

npm run start -- --entryFile repl

Also consider add .nestjs_repl_history to .gitignore