nest 命令

@nestjs/cli 工具使用方法

Posted by chanweiyan on April 25, 2026

@nest/cli 工程化

  • nest new 创建项目
  • nest g 生成代码
  • nest build 编译构建
  • nest watch 开发时 live-reload
  • nest info 查看项目信息

直接 npx 执行,npm 会把它下载下来,然后,执行

1
npx @nestjs/cli new 项目名

推荐:安装到全局,然后,执行

1
2
3
4
5
6
7
npm install -g @nestjs/cli

nest new 项目名

# 更新版本
# npm update -g @nestjs/cli

nest -h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
➜  nest_proj git:(main) ✗ nest -h
Usage: nest <command> [options]

Options:
  -v, --version                                   Output the current version.
  -h, --help                                      Output usage information.

Commands:
  new|n [options] [name]                          Generate Nest application.
  build [options] [app]                           Build Nest application.
  start [options] [app]                           Run Nest application.
  info|i                                          Display Nest project details.
  add [options] <library>                         Adds support for an external library to your project.
  generate|g [options] <schematic> [name] [path]  Generate a Nest element.
    Schematics available on @nestjs/schematics collection:
      ┌───────────────┬─────────────┬──────────────────────────────────────────────┐
      │ name          │ alias       │ description                                  │
      │ application   │ application │ Generate a new application workspace         │
      │ class         │ cl          │ Generate a new class                         │
      │ configuration │ config      │ Generate a CLI configuration file            │
      │ controller    │ co          │ Generate a controller declaration            │
      │ decorator     │ d           │ Generate a custom decorator                  │
      │ filter        │ f           │ Generate a filter declaration                │
      │ gateway       │ ga          │ Generate a gateway declaration               │
      │ guard         │ gu          │ Generate a guard declaration                 │
      │ interceptor   │ itc         │ Generate an interceptor declaration          │
      │ interface     │ itf         │ Generate an interface                        │
      │ library       │ lib         │ Generate a new library within a monorepo     │
      │ middleware    │ mi          │ Generate a middleware declaration            │
      │ module        │ mo          │ Generate a module declaration                │
      │ pipe          │ pi          │ Generate a pipe declaration                  │
      │ provider      │ pr          │ Generate a provider declaration              │
      │ resolver      │ r           │ Generate a GraphQL resolver declaration      │
      │ resource      │ res         │ Generate a new CRUD resource                 │
      │ service       │ s           │ Generate a service declaration               │
      │ sub-app       │ app         │ Generate a new application within a monorepo │
      └───────────────┴─────────────┴──────────────────────────────────────────────┘

nest new -h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
➜  nest_proj git:(main) ✗ nest new -h
Usage: nest new|n [options] [name]

Generate Nest application.

Options:
  --directory [directory]                 Specify the destination directory
  -d, --dry-run                           Report actions that would be performed without writing out results. (default: false)
  -g, --skip-git                          Skip git repository initialization. (default: false)
  -s, --skip-install                      Skip package installation. (default: false)
  -p, --package-manager [packageManager]  Specify package manager.
  -l, --language [language]               Programming language to be used (TypeScript or JavaScript) (default: "TypeScript")
  -c, --collection [collectionName]       Schematics collection to use (default: "@nestjs/schematics")
  --strict                                Enables strict mode in TypeScript. (default: false)
  -h, --help                              Output usage information.

--strict 指定 typescript 的编译选项是否开启严格模式,对应下面5个选项

1
2
3
4
5
6
7
8
9
// tsconfig.json
{
  "skipLibCheck": true,
  "strictNullChecks": false,
  "noImplicitAny": false,
  "strictBindCallApply": false,
  "forceConsistentCasingInFileNames": false,
}

nest g -h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
➜  nest_proj git:(main) ✗ nest g -h
Usage: nest generate|g [options] <schematic> [name] [path]

Generate a Nest element.
  Schematics available on @nestjs/schematics collection:
    ┌───────────────┬─────────────┬──────────────────────────────────────────────┐
    │ name          │ alias       │ description                                  │
    │ application   │ application │ Generate a new application workspace         │
    │ class         │ cl          │ Generate a new class                         │
    │ configuration │ config      │ Generate a CLI configuration file            │
    │ controller    │ co          │ Generate a controller declaration            │
    │ decorator     │ d           │ Generate a custom decorator                  │
    │ filter        │ f           │ Generate a filter declaration                │
    │ gateway       │ ga          │ Generate a gateway declaration               │
    │ guard         │ gu          │ Generate a guard declaration                 │
    │ interceptor   │ itc         │ Generate an interceptor declaration          │
    │ interface     │ itf         │ Generate an interface                        │
    │ library       │ lib         │ Generate a new library within a monorepo     │
    │ middleware    │ mi          │ Generate a middleware declaration            │
    │ module        │ mo          │ Generate a module declaration                │
    │ pipe          │ pi          │ Generate a pipe declaration                  │
    │ provider      │ pr          │ Generate a provider declaration              │
    │ resolver      │ r           │ Generate a GraphQL resolver declaration      │
    │ resource      │ res         │ Generate a new CRUD resource                 │
    │ service       │ s           │ Generate a service declaration               │
    │ sub-app       │ app         │ Generate a new application within a monorepo │
    └───────────────┴─────────────┴──────────────────────────────────────────────┘

Options:
  -d, --dry-run                      Report actions that would be taken without writing out results.
  -p, --project [project]            Project in which to generate files. - monorepo
  --flat                             Enforce flat structure of generated element.
  --no-flat                          Enforce that directories are generated.
  --spec                             Enforce spec files generation. (default: true)
  --spec-file-suffix [suffix]        Use a custom suffix for spec files.
  --skip-import                      Skip importing (default: false)
  --no-spec                          Disable spec files generation.
  -c, --collection [collectionName]  Schematics collection to use.
  -h, --help                         Output usage information.

nest build -h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
➜  nest_proj git:(main) ✗ nest build -h
Usage: nest build [options] [app]

Build Nest application.

Options:
  -c, --config [path]   Path to nest-cli configuration file.
  -p, --path [path]     Path to tsconfig file.
  -w, --watch           Run in watch mode (live-reload).
  --watchAssets         Watch non-ts (e.g., .graphql) files mode.
  --webpack             Use webpack for compilation.
  --webpackPath [path]  Path to webpack configuration.
  --tsc                 Use tsc for compilation.
  -h, --help            Output usage information.

--wepback--tsc 是指定用什么编译,默认是 tsc 编译,也可以切换成 webpack

nest-cli schema

nest start -h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
➜  nest_proj git:(main) ✗ nest start -h
Usage: nest start [options] [app]

Run Nest application.

Options:
  -c, --config [path]        Path to nest-cli configuration file.
  -p, --path [path]          Path to tsconfig file.
  -w, --watch                Run in watch mode (live-reload).
  --watchAssets              Watch non-ts (e.g., .graphql) files mode.
  -d, --debug [hostport]     Run in debug mode (with --inspect flag).
  --webpack                  Use webpack for compilation.
  --webpackPath [path]       Path to webpack configuration.
  --tsc                      Use tsc for compilation.
  --sourceRoot [sourceRoot]  Points at the root of the source code for the single project in standard mode structures, or the default project in monorepo mode structures.
  --entryFile [entryFile]    Path to the entry file where this command will work with. Defaults to the one defined at your Nest's CLI config file.
  -e, --exec [binary]        Binary to run (default: "node").
  --preserveWatchOutput      Use "preserveWatchOutput" option when tsc watch mode.
  -h, --help                 Output usage information.


nest info

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
➜  nest_proj git:(main) ✗ nest info

 _   _             _      ___  _____  _____  _     _____
| \ | |           | |    |_  |/  ___|/  __ \| |   |_   _|
|  \| |  ___  ___ | |_     | |\ `--. | /  \/| |     | |
| . ` | / _ \/ __|| __|    | | `--. \| |    | |     | |
| |\  ||  __/\__ \| |_ /\__/ //\__/ /| \__/\| |_____| |_
\_| \_/ \___||___/ \__|\____/ \____/  \____/\_____/\___/


[System Information]
OS Version     : macOS Unknown
NodeJS Version : v20.2.0
YARN Version    : 1.22.22

[Nest CLI]
Nest CLI Version : 9.5.0

[Nest Platform Information]
platform-express version : 9.4.3
mapped-types version     : 2.1.1
schematics version       : 9.2.0
testing version          : 9.4.3
common version           : 9.4.3
core version             : 9.4.3
cli version              : 9.5.0