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(); |
Start the repl using npm run console
. Then you could do something like
> app.get('MyService')
Thank you so much, with your instructions I could debug my application.
Thanks!
Thanks for sharing, here the one liner dependencies install:
npm install purdy repl await-outside --save-dev
Awesome, this is something I've really missed from other frameworks.
Awesome, thanks
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
In
package.json
add"console": "ts-node -r tsconfig-paths/register src/console.ts",