Skip to main content
Back to blog// general

Mastering NestJS CLI: Advanced Usage - From Beginner to Pro

New to NestJS, look at NestJS 101 to get up and running. NestJS is a powerful and versatile framework for building scalable and maintainable server-side applications. One key tool that makes working w

NK

Nicanor Korir

Author

August 6, 2024
5 min read
nestjs-clinestjsbackend

New to NestJS, look at NestJS 101 to get up and running.

NestJS is a powerful and versatile framework for building scalable and maintainable server-side applications. One key tool that makes working with NestJS so efficient is the NestJS CLI. Let's dive into basics, tips and tricks, NestJS capabilities, custom schematics, code generation, configuration and automation scripts

Introduction to NestJS CLI

The NestJS CLI was developed to streamline the development process of NestJS applications, drawing inspiration from the Angular CLI. It provides a set of powerful commands to automate routine tasks, ensuring that developers can focus more on building features rather than configuring their environment. With the NestJS CLI, you can quickly generate boilerplate code, manage dependencies, and run your application with ease.

Basic Commands

  • Creating a new project:

```typescript

nest new project-name

```

  • Running the development server:

```typescript

npm run start:dev

```

Pros and Cons of NestJS CLI

Pros

  • Efficiency: Automates repetitive tasks, saving time and reducing human error.
  • Consistency: Ensures a consistent project structure and coding standards across the team.
  • Speed: Accelerates development with quick scaffolding of components.
  • Integration: Seamlessly integrates with TypeScript and other NestJS features.
  • Customization: Allows the creation of custom schematics to fit specific project needs.

Cons

  • High Learning Curve
  • Overhead: May introduce some overhead for very small projects where a simple setup would suffice.
  • Dependency: Heavy reliance on CLI can sometimes obscure understanding of underlying processes.

Setting Up NestJS CLI

Before you can start using the NestJS CLI, you need to install it globally on your machine.

TypeScript
typescript

Creating a New Project

To create a new NestJS project, use the following command:

TypeScript
typescript

This command sets up a new NestJS project with a default structure, installs the necessary dependencies, and provides an initial set of files.

Code Generation with NestJS CLI

One of the most powerful features of the NestJS CLI is its ability to generate boilerplate code for you. This can save a lot of time and ensure that your code adheres to best practices.

Generating Modules

Bash
bash

Generating Controllers

Bash
bash

Generating Services

Bash
bash

Generating Middleware, Guards, Pipes, and Interceptors

Bash
bash
1
2
3
4

Custom Schematics

Schematics in NestJS allow you to define custom templates for generating code. This can be incredibly useful for automating repetitive tasks and enforcing coding standards across your team. Custom schematics extend the capabilities of the default CLI commands by enabling you to create your own file structures, code patterns, and configurations tailored to your project requirements.

Creating Custom Schematics

  1. Install the NestJS Schematics CLI:

```typescript

npm install -g @nestjs/schematics

```

  1. Create a new schematics collection:

```typescript

schematics blank --name my-schematics

```

  1. Define your custom schematic: Modify the files in the generated my-schematics folder to define your custom schematic. This typically involves editing the index.ts and schema.json files.
  1. Build and test your schematics:

```bash

npm run build

schematics .:my-schematic

```

Example: Custom Service Schematic

TypeScript
typescript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

Project Configuration

The nest-cli.json file is the central configuration file for a NestJS project. It allows you to customize various aspects of your project, such as source roots, compiler options, and collection paths. Proper configuration ensures that your project is set up according to your specific requirements and can help streamline development processes.

Configuring the Project

Change Source Root: Specify the root directory of your source files.

TypeScript
typescript
1
2
3

Add Compiler Options

Include additional compiler options to customize the build process.

TypeScript
typescript
1
2
3
4
5
6

Example nest-cli.json

TypeScript
typescript
1
2
3
4
5
6
7
8
9

Automation Scripts

Automation scripts can help you streamline your development workflow by automating repetitive tasks. You can define these scripts in your package.json file.

Example: Automating Linting and Testing

JSON
json
1
2
3
4
5

Speed Up Development

Use the --dry-run flag: Preview the changes a command will make without actually applying them.

JavaScript
javascript

Keep Dependencies Updated

Regularly check for outdated packages and update them to keep your project secure and up-to-date.

Bash
bash
1
2

Utilize Aliases

Shorten CLI commands: Create aliases for frequently used commands to speed up your workflow.

JSON
json
1
2
3
4
5

The NestJS CLI is a powerful tool that can significantly improve your development workflow. By mastering its advanced features, such as custom schematics, project configuration, and automation scripts, you can streamline your processes and focus on building robust applications.

Further Reading

Stay in the Loop

Get occasional updates on AI engineering, robotics projects, and lessons from building startups. No spam, unsubscribe anytime.