NestJS CLI Commands and Usage

By Łukasz Kallas
Picture of the author
Published on
nestjs image

Why Use the NestJS CLI?

The NestJS CLI provides developers with the ability to scaffold projects quickly, generate various types of files, and maintain consistent coding practices. It automates repetitive tasks, allowing you to focus on writing your business logic. Some benefits include:

  • Quick Project Setup: Initialize new projects with best practices.
  • Code Generation: Generate components like modules, controllers, services, and more with a single command.
  • Scaffolding: Maintain a consistent project structure.
  • Testing: Simplify testing with auto-generated test files.

Installing the NestJS CLI

Before using the CLI, you need to install it globally on your system. Run the following command in your terminal:

npm install -g @nestjs/cli

Once installed, you can use the nest command to perform various actions.

Commonly Used NestJS CLI Commands

Below are some of the most frequently used NestJS CLI commands and their usage:

  1. Creating a New Project

To start a new NestJS project, use the new command. This command creates a new directory with the project structure and installs the necessary dependencies.

nest new

The CLI will prompt you to choose the package manager, project name etc.

  1. Generating Modules

Modules in NestJS organize your application into cohesive blocks of functionality. Use the generate module command to create a new module:

nest generate module users

users: This is the name of the module you want to create. The CLI creates a users module file under the specified path.

  1. Generating Controllers

Controllers handle incoming requests and return responses to the client. To generate a new controller, use:

nest generate controller users

users: Replace with the name of the controller. It generates a users.controller.ts file in the appropriate module directory.

  1. Generating Services

Services contain the business logic and can be shared across controllers. To generate a service, run:

nest generate service users

users: This will create a users.service.ts file.

  1. Generating Other Components

NestJS CLI also supports generating additional components like guards, interceptors, pipes, and more. Here’s an example of generating a guard:

nest generate guard auth

auth: Generates an auth.guard.ts file.

  1. Building the Application

You can compile your application using the build command. It uses the TypeScript compiler to transpile your TypeScript files into JavaScript.

nest build

This command outputs compiled files to the dist directory by default.

  1. Running the Application

To run the application in development mode with hot-reload support, use the start command:

nest start --watch

The --watch flag enables hot-reloading. The application restarts automatically on file changes. 8. Testing the Application

Stay Tuned

Want to learn?
The best articles, links and news related to software development delivered once a week to your inbox.