Last active
October 4, 2022 00:12
-
-
Save royshouvik/2cba46b56dd70529731a068eb82ca538 to your computer and use it in GitHub Desktop.
Nestjs REPL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
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");
.
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
Awesome, thanks