Skip to content

Instantly share code, notes, and snippets.

@royshouvik
Last active October 4, 2022 00:12
Show Gist options
  • Star 37 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save royshouvik/2cba46b56dd70529731a068eb82ca538 to your computer and use it in GitHub Desktop.
Save royshouvik/2cba46b56dd70529731a068eb82ca538 to your computer and use it in GitHub Desktop.
Nestjs REPL
import 'dotenv/config';
import { NestFactory } from '@nestjs/core';
import * as repl from 'repl';
import * as Logger from 'purdy';
const LOGGER_OPTIONS = {
indent: 2,
depth: 1,
};
class InteractiveNestJS {
async run() {
// create the application context
const targetModule = require(`${__dirname}/app.module`);
const applicationContext = await NestFactory.createApplicationContext(
// tslint:disable-next-line: no-string-literal
targetModule['AppModule'],
);
const awaitOutside = require('await-outside');
// start node repl
const server = repl.start({
useColors: true,
prompt: '> ',
writer: replWriter,
ignoreUndefined: true,
});
server.context.app = applicationContext;
awaitOutside.addAwaitOutsideToReplServer(server);
}
}
function replWriter(value: object): string {
return Logger.stringify(value, LOGGER_OPTIONS);
}
const session = new InteractiveNestJS();
session.run();
@royshouvik
Copy link
Author

In package.json add "console": "ts-node -r tsconfig-paths/register src/console.ts",

@royshouvik
Copy link
Author

Start the repl using npm run console . Then you could do something like

> app.get('MyService')

@williampsena
Copy link

williampsena commented May 10, 2020

Thank you so much, with your instructions I could debug my application.

@drKreso
Copy link

drKreso commented Jun 30, 2020

Thanks!

@fidelisrafael
Copy link

Thanks for sharing, here the one liner dependencies install:

npm install purdy repl await-outside --save-dev

@arosemena
Copy link

Awesome, this is something I've really missed from other frameworks.

@sinaabadi
Copy link

Awesome, thanks

@ashug0001
Copy link

Hey, I tried to use this in NestJS 8, but I'm getting an error Nest could not find AppService element (this provider does not exist in the current context) when i do let a = await app.get("AppService");.

@pavlikm
Copy link

pavlikm commented Sep 9, 2022

I have the same error as ashug0001 :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment