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.
npm install nestjs-consoleVerified import paths — ran on the pinned version, not inferred.
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.
Change `@Console({ name: "myCommand" })` to `@Console({ command: "myCommand" })`.Adjust command handler methods to accept an `options` object as their second parameter. Refer to the updated documentation or examples for the new signature.
Upgrade your Node.js installation to version 20.0.0 or later. Use a tool like `nvm` for managing multiple Node.js versions if needed.
Always install `nestjs-console` version that explicitly supports your installed NestJS and `commander` versions. Check the `nestjs-console` changelog or `package.json` peer dependencies for compatibility. Use `npm install` or `yarn add` to automatically resolve compatible versions where possible.
Ensure your `tsconfig.json` `module` and `moduleResolution` settings (e.g., `NodeNext` or `Node16`) are appropriate for your project's module system. If using ESM-only dependencies, consider dynamic `import()` or configuring your NestJS project to build as ESM (though not officially supported by NestJS core).
Ensure all services, modules, or providers that `YourConsoleClass` depends on are correctly imported into `AppModule` (or any module that provides `YourConsoleClass`). Verify `providers` and `imports` arrays in your `@Module` decorator.
Check that your console class has the `@Console` decorator, your command methods have the `@Command` decorator, and that your console class is listed in the `providers` array of your `AppModule` or an imported module.
Refactor your imports to use ES module `import` syntax. If you need to import a CommonJS module in an ESM context, Node.js typically handles it, but for ESM-only modules in a CommonJS context, you might need dynamic `import()`.
Ensure you are using `import { Console } from 'nestjs-console';` and that your `tsconfig.json` correctly processes decorators and modules. Verify your Node.js version meets the package requirements.