Skip to content

Instantly share code, notes, and snippets.

@fabecerram
Created December 6, 2022 21:26
Show Gist options
  • Save fabecerram/81d6d1e8eb2db951fd0c739eb52a5635 to your computer and use it in GitHub Desktop.
Save fabecerram/81d6d1e8eb2db951fd0c739eb52a5635 to your computer and use it in GitHub Desktop.
NestJs CLI Cheat Sheet – A Basic Guide to NestJs CLI

NestJs CLI Cheat Sheet – A Basic Guide to NestJs CLI

The information in this guide is of an educational nature, it is mainly oriented to students and developers who start with NestJs, this compilation has been selected thinking about the necessary aspects for the basic and intermediate level courses, so it does not cover in depth each one of the mentioned aspects, only covers the most common characteristics in them.

If you want to go into more details, please check the CLI Command Reference at https://docs.nestjs.com/cli/overview


Preparing the Environment

Checking Currently Installed Version of NestJs

nest --version      
nest -v    

Installing NestJs CLI

npm install -g @nestjs/cli

npm install -g @nestjs/cli@latest

npm install -g @nestjs/cli@version

Uninstalling NestJs CLI

npm uninstall -g @nestjs/cli

Cleaning the Cache

npm cache clean --force


Using Online Help

nest --help

nest -h

nest <command> --help


CLI Basic Commands

CLI Command Language Syntax

nest [command] [args] [options]

- Most commands, and some options, have aliases.
- Arguments are not prefixed.
- Option names are prefixed with a double dash (--) characters. 
- Option aliases are prefixed with a single dash (-) character.

Example:

    nest build my-app -c production
    nest build my-app --configuration production
    nest b my-app -c production
    nest b my-app --configuration production

Create New Project

nest new projectName
nest n projectName

Get Informacion About an Application

Displays information about installed nest packages and other helpful system info.

nest info
nest i      

Run an NestJs Application

Builds and serves your application, rebuilding on file changes.

nest start
nest start projectName

# Run in watch mode (live-reload).
nest start -w
nest start --watch

Build an Application

Compiles an application or library into an output directory at the given output path.

nest build
nest b  
nest build projectName    

Run Linting Tools

NestJs does not include this functionality as part of the CLI, but we can execute it through NPM, since NestJs uses Lint, it is possible to see that the corresponding script is added to the package.json

npm lint   
npm lint projectName

Run Unit Tests

NestJs does not include this functionality as part of the CLI, but we can execute it through NPM, since NestJs uses Jest, it is possible to see that the corresponding script is added to the package.json

npm test   
npm test projectName


Generate Command

Generate command in NestJs CLI is one of the building blocks of NestJs applications. It makes use of templates called schemas, which make it easy to create different elements quickly and safely.

Syntax

nest generate <schematic>      
nest g <schematic>

Create a New Class

nest generate class [name]
nest generate cl [name]
nest g class [name]
nest g cl [name]

# Do not create "spec.ts" test files for the new class.
nest generate class [name] --no-spec

Create Controller

nest generate controller [name]
nest generate co [name]
nest g controller [name]
nest g co [name]

# Do not create "spec.ts" test files for the new controller.
nest generate controller [name] --no-spec

Create Decorator

nest generate decorator [name]
nest generate d [name]
nest g decorator [name]
nest g d [name]

Create Filter

nest generate filter [name]
nest generate f [name]
nest g filter [name]
nest g f [name]

Create Guard

nest generate guard [name]
nest generate gu [name]
nest g guard [name]
nest g gu [name]

Create Interceptor

nest generate interceptor [name]
nest generate itc [name]
nest g interceptor [name]
nest g itc [name]

Create Interface

nest generate interface [name]
nest generate itf [name]
nest g interface [name]
nest g itf [name]

Create Modules

nest generate module [name]
nest generate mo [name]
nest g module [name]
nest g mo [name]

Create Pipes

nest generate pipe [name]
nest generate pi [name]
nest g pipe [name]
nest g pi [name]

# Do not create "spec.ts" test files for the new pipe.
nest generate pipe [name] --no-spec

Create Resource

Generate a new CRUD resource that contains all the NestJS building blocks (module, service, controller classes) but also an entity class, DTO classes as well as the testing (.spec) files.

nest generate resource [name]
nest generate res [name]
nest g resource [name]
nest g res [name]

# Do not create "spec.ts" test files for the new resource.
nest generate resource [name] --no-spec

Create Services

nest generate service [name]
nest generate s [name]
nest g service [name]
nest g s [name]

# Do not create "spec.ts" test files for the new service.
nest generate service [name] --no-spec


Creator

Fabian A. Becerra M. https://github.com/fabecerram


Copyright and license

Code and documentation copyright 2019-2022 the authors. Code released under the MIT License.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment