Skip to content

Instantly share code, notes, and snippets.

@akwodkiewicz
Created November 6, 2020 13:27
Show Gist options
  • Save akwodkiewicz/a18a7de37bf3dce331825a1311054232 to your computer and use it in GitHub Desktop.
Save akwodkiewicz/a18a7de37bf3dce331825a1311054232 to your computer and use it in GitHub Desktop.
NestJs in node REPL
// For this trick you'll need:
//
// $ node --experimental-repl-await
//
// If you happen to use Typescript alias paths, you'll also want to install `tsconfig-paths`
// and actually run the node REPL with:
//
// $ TS_NODE_PROJECT=path/to/tsconfig.json node -r tsconfig-paths/register --experimental-repl-await
//
// All the statements need to be input inside REPL.
let { NestFactory } = require('@nestjs/core')
// importing whole app module
let { AppModule } = require('./dist/src/app.module')
// instantiating whole app (module)
appModule = await NestFactory.create(AppModule);
// extracting particular services
someService = appModule.get('SomeService');
// If you want to use single services which have other dependencies, you will need
// to import a *service* (not a module) and inject the dependencies yourself.
let { MyServiceWhichNeedsHttpService } = require('./dist/src/something')
let { HttpModule } = require('@nestjs/common')
httpModule = await NestFactory.create(HttpModule);
httpService = httpModule.get('HttpService');
myService = new MyServiceWhichNeedsHttpService(httpService);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment