Skip to content

Nest CLI

The Nest CLI does far more than scaffold a new project, it can generate modules, controllers, services, and even a complete CRUD resource with a single command. This lesson covers the commands you'll use daily.

Generating Building Blocks with nest generate

Every NestJS building block, module, controller, service, guard, pipe, interceptor, has a matching schematic. Running nest generate <schematic> <name> creates the file, applies the correct decorator boilerplate, and automatically wires it into the nearest module.

nest generate module users
nest generate controller users
nest generate service users

Each command can be shortened, nest g mo users, nest g co users, nest g s users, and Nest automatically updates UsersModule to register the new controller and service.

Common Schematic Aliases

nest g mo <name>    // module
nest g co <name>    // controller
nest g s <name>     // service
nest g gu <name>    // guard
nest g pi <name>    // pipe
nest g in <name>    // interceptor
nest g f <name>     // exception filter
nest g resource <name>  // full CRUD resource
  • nest g resource <name> scaffolds a module, controller, service, and DTOs for a complete CRUD feature.
  • Adding --no-spec skips generating a matching .spec.ts test file.
  • The CLI respects your project's nest-cli.json settings for source root and generation style.
  • Generated files are automatically declared in the nearest existing module.

Nest CLI Command Reference

The commands you'll reach for most while building a Nest application.

Command Purpose
nest new <name> Create a new project
nest generate module <name> Generate a feature module
nest generate controller <name> Generate a controller
nest generate service <name> Generate a service
nest generate resource <name> Generate a full CRUD resource
nest build Compile the project
nest start --watch Run with file-watching enabled
nest info Print installed Nest package versions

Generating a Full CRUD Resource

nest generate resource is the fastest way to start a new feature. It asks whether you want a REST API, GraphQL, microservice, or WebSocket transport layer, then generates a module, controller, service, entity, and DTO classes pre-wired with CRUD method stubs.

nest g resource orders
# ? What transport layer do you use? REST API
# ? Would you like to generate CRUD entry points? Yes

The generated controller already includes create, findAll, findOne, update, and remove methods mapped to the correct HTTP verbs, you fill in the service logic.

Monorepo Mode

The Nest CLI also supports a monorepo mode (nest generate app and nest generate library) for projects with multiple deployable applications sharing common libraries, useful when a REST API and a microservice worker live in the same repository.

Common Mistakes

  • Manually creating boilerplate files instead of using schematics, then forgetting to register them in a module.
  • Not realizing nest generate resource already wires up basic CRUD, and rewriting it from scratch.
  • Running CLI commands from the wrong directory, outside the project root.
  • Ignoring the interactive prompts (transport layer, CRUD stubs) and missing useful generated code.

Key Takeaways

  • nest generate (or nest g) scaffolds modules, controllers, services, and more with correct wiring.
  • nest generate resource produces an entire CRUD feature in one command.
  • Short aliases like mo, co, and s speed up day-to-day CLI usage.
  • Monorepo mode supports multiple apps and shared libraries in one Nest workspace.

Pro Tip

Run nest g resource <name> --no-spec when prototyping quickly, then add real spec files once the feature's shape has stabilized, this avoids maintaining placeholder tests for code that's still changing.