Anik
Anik

Reputation: 2880

Nestjs REPL on Nx

trying to run repl in Nx, following the tutorial here https://docs.nestjs.com/recipes/repl, So I have a repl.ts file,

import { repl } from '@nestjs/core';
import { AppModule } from './app/app.module';

async function bootstrap() {
  await repl(AppModule);
}

bootstrap();

and if I try to run the repl using nx serve myapp --entryFile repl it gives an error,

> nx run core-service:serve --entryFile=repl 
'entryFile' is not found in schema

dependencies:

"@nrwl/nest": "14.7.13",
"@nestjs/core": "9.1.2",

Upvotes: 2

Views: 1196

Answers (3)

exsesx
exsesx

Reputation: 51

I think the best way to get REPL up and running is to make the following changes to the project.json file:

  1. Add repl configuration to the build target.
"build": {
  ...
  "configurations": {
    ...
    "repl": {
      "main": "apps/your-app/src/repl.ts"
    }
  }
}
  1. Add a new repl target to the targets property (you can name it any way you want)
"repl": {
  "executor": "@nx/node:node",
  "options": {
    "buildTarget": "your-app:build:repl"
  }
}

This way, you don't need to copy and change the build target. Instead, you're reusing it with minor modifications.

Upvotes: 0

Jelle Licht
Jelle Licht

Reputation: 11

Update your serve target:

"repl": {
  "buildTarget": "<project>:build",
  "buildTargetOptions": {
    "main": "packages/<project>/src/repl.ts"
  }
}

Now you can use npx nx run project:serve:repl to get a REPL that restarts when any of the code changes. Note that the executor breaks some REPL features, such as using control+D to close the REPL.

Upvotes: 1

user11537130
user11537130

Reputation: 296

Add your repl.ts beside main.ts

inside your apps/{project}/project.json

on the targets property add this

    "repl": { // Add this code
      "executor": "@nrwl/webpack:webpack",
      "outputs": ["{options.outputPath}"],
      "options": {
        "target": "node",
        "compiler": "tsc",
        "outputPath": "dist/apps/project",
        "main": "apps/project/src/repl.ts", // take note of this
        "tsConfig": "apps/server/tsconfig.app.json",
        "assets": []
      },
      "configurations": {
        "production": {
          "optimization": true,
          "extractLicenses": true,
          "inspect": false,
          "fileReplacements": [
            {
              "replace": "apps/project/src/environments/environment.ts",
              "with": "apps/project/src/environments/environment.prod.ts"
            }
          ]
        }
      }

Update the serve property (still on targets)

"repl": {
    "buildTarget": "server:repl"
}

Your schema should look like this

{
  "$schema": "../../node_modules/nx/schemas/project-schema.json",
  "sourceRoot": "apps/project/src",
  "projectType": "application",
  "targets": {
    "build": {...}, // Minimized code
    "repl": { // Add this code
      "executor": "@nrwl/webpack:webpack",
      "outputs": ["{options.outputPath}"],
      "options": {
        "target": "node",
        "compiler": "tsc",
        "outputPath": "dist/apps/project",
        "main": "apps/project/src/repl.ts", // take note of this
        "tsConfig": "apps/server/tsconfig.app.json",
        "assets": []
      },
      "configurations": {
        "production": {
          "optimization": true,
          "extractLicenses": true,
          "inspect": false,
          "fileReplacements": [
            {
              "replace": "apps/project/src/environments/environment.ts",
              "with": "apps/project/src/environments/environment.prod.ts"
            }
          ]
        }
      }
    },
    "serve": {
      "executor": "@nrwl/js:node",
      "options": {
        "buildTarget": "server:build"
      },
      "configurations": {
        "production": {
          "buildTarget": "server:build:production"
        },
        "repl": { // add this on your serve property
          "buildTarget": "server:repl"
        }
      }
    },
    //
}

You can now run the repl by running

npx nx run project:serve:repl

Upvotes: 3

Related Questions