Skip to content

Instantly share code, notes, and snippets.

@royshouvik
Last active October 4, 2022 00:12
Show Gist options
  • 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();
@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