Skip to main content

Console Commands

We use NestJS Console to create console commands. Console commands can be used locally or deployed.

Defining Commands

Demo console command
import { MikroORM, UseRequestContext } from "@mikro-orm/core";
import { Injectable, Logger } from "@nestjs/common";
import { Command, Console } from "nestjs-console";

export class DemoConsole {
private readonly logger = new Logger(;

private readonly orm: MikroORM, // MikroORM is injected so we can use the request context
) {}

command: "demo-command",
description: "Demo-Command for cronjob-deployment",
async demoCommand(): Promise<void> {
this.logger.log("Execute demo-command.");

Listing all available commands


npm run console


npm run console:prod

Running commands


npm run console demo-command


npm run console:prod demo-command

Best practices

  • Dangerous commands (e.g. resetting the database) should check the NODE_ENV and only run locally.
async execute(): Promise<void> {
if (process.env.NODE_ENV !== "development") {
this.logger.error("Not allowed in production environments");

// ...