{"library":"nestjs-console","title":"NestJS Console Module","description":"nestjs-console is a NestJS module that provides a command-line interface (CLI) for NestJS applications. It enables developers to define and run console commands within the application's dependency injection context, which is particularly useful for tasks such as headless operations, cron jobs, data processing, and migrations. The module integrates with the popular `commander.js` package for robust command parsing and execution. It bootstraps a `NestApplicationContext` (headless) rather than a full `NestApplication`, ensuring that CLI commands have access to all necessary NestJS services without initiating an HTTP server. The current stable version is 10.0.0, which supports NestJS v11, Commander v12 & v13, and requires Node.js >= v20.0.0. Its release cadence is closely tied to major NestJS and Commander updates. A key differentiator is its seamless integration with NestJS's decorators and DI system, allowing command logic to reside directly within NestJS providers.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install nestjs-console"],"cli":null},"imports":["import { ConsoleModule } from 'nestjs-console';","import { Console } from 'nestjs-console';","import { Command } from 'nestjs-console';","import { ConsoleService } from 'nestjs-console';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { Module } from '@nestjs/common';\nimport { NestFactory } from '@nestjs/core';\nimport { ConsoleModule, Console, Command, Option, createCommanderAction } from 'nestjs-console';\n\ninterface MyCommandOptions {\n  name: string;\n  count: number;\n}\n\n@Console({\n  command: 'app',\n  description: 'Application commands'\n})\nexport class AppConsole {\n  @Command({\n    command: 'hello <message>',\n    description: 'Says hello with a message',\n    options: [\n      { flags: '-n, --name <name>', description: 'Your name', required: true },\n      { flags: '-c, --count <count>', description: 'Number of times to say hello', defaultValue: 1, parser: (val) => parseInt(val, 10) }\n    ]\n  })\n  async hello(message: string, options: MyCommandOptions) {\n    for (let i = 0; i < options.count; i++) {\n      console.log(`Hello ${options.name || 'Stranger'}! You said: \"${message}\"`);\n    }\n    console.log(`\nEnvironment: ${process.env.NODE_ENV || 'development'}`);\n  }\n\n  @Command({\n    command: 'greet [recipient]',\n    description: 'Greets a recipient or the world'\n  })\n  async greet(recipient?: string) {\n    console.log(`Greetings, ${recipient || 'world'}!`);\n  }\n}\n\n@Module({\n  imports: [ConsoleModule.forRoot({\n    handleExceptions: true,\n    commander: createCommanderAction()\n  })],\n  providers: [AppConsole]\n})\nexport class AppModule {}\n\nasync function bootstrap() {\n  try {\n    const app = await NestFactory.createApplicationContext(AppModule, {\n      logger: ['error', 'warn'], // Only log errors and warnings for console app\n    });\n    await app.select(ConsoleModule).get(ConsoleService).init();\n    await app.close();\n  } catch (e) {\n    console.error('Console application failed to bootstrap:', e);\n    process.exit(1);\n  }\n}\n\n// To run: \n// 1. Compile: `npm run build` or `tsc`\n// 2. Execute: `node dist/console.js app hello \"How are you?\" --name Alice -c 3`\n//    or `node dist/console.js app greet Bob`\n//    or `node dist/console.js --help`\n//    Make sure 'console.ts' is set as the entry point in your tsconfig/build process.\nbootstrap();","lang":"typescript","description":"Demonstrates setting up a basic NestJS console application, defining a command class using `@Console`, and command methods using `@Command` and `@Option` decorators. It shows how to pass arguments and options, including type parsing, and how to bootstrap the application context for CLI execution.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}