跳转至

概述

Nest CLI是一个命令行界面工具,可以帮助您初始化、开发和维护 Nest 应用程序。 它以多种方式提供帮助,包括搭建项目、以开发模式为其提供服务,以及构建和捆绑应用程序以进行生产分发。 它体现了最佳实践架构模式,以鼓励结构良好的应用程序。

安装

Note

在本指南中,我们将介绍如何使用npm安装包,包括Nest CLI。 您可以自行决定使用其他包管理器。 使用npm,你有几个可用的选项来管理你的OS命令行如何解析nest CLI二进制文件的位置。 在这里,我们描述了使用-g选项全局安装nest二进制文件。 这提供了一定程度的便利,也是我们在整个文档中所采用的方法。 请注意,在全局安装 任何 npm包时,确保它们运行正确版本的责任由用户负责。 这也意味着如果你有不同的项目,每个项目将运行相同的CLI版本。 一个合理的替代方案是使用npx程序(或其他包管理器的类似功能),以确保您运行的是一个 托管版本的 Nest CLI。 我们建议您咨询npx文档和/或您的DevOps支持人员以获得更多信息。

使用npm install -g命令全局安装 CLI(关于全局安装的详细信息请参见上面的 Note)。

$ npm install -g @nestjs/cli

基本工作流程

安装后,您可以通过nest可执行文件直接从 OS 命令行调用 CLI 命令。 通过输入以下内容,查看可用的nest命令:

$ nest --help

Get help on an individual command using the following construct. Substitute any command, like new, add, etc., where you see generate in the example below to get detailed help on that command:

$ nest generate --help

To create, build and run a new basic Nest project in development mode, go to the folder that should be the parent of your new project, and run the following commands:

1
2
3
$ nest new my-nest-project
$ cd my-nest-project
$ npm run start:dev

In your browser, open http://localhost:3000 to see the new application running. The app will automatically recompile and reload when you change any of the source files.

项目结构

When you run nest new, Nest generates a boilerplate application structure by creating a new folder and populating an initial set of files. You can continue working in this default structure, adding new components, as described throughout this documentation. We refer to the project structure generated by nest new as standard mode. Nest also supports an alternate structure for managing multiple projects and libraries called monorepo mode .

Aside from a few specific considerations around how the build process works (essentially, monorepo mode simplifies build complexities that can sometimes arise from monorepo-style project structures), and built-in library support, the rest of the Nest features, and this documentation, apply equally to both standard and monorepo mode project structures. In fact, you can easily switch from standard mode to monorepo mode at any time in the future, so you can safely defer this decision while you're still learning about Nest.

You can use either mode to manage multiple projects. Here's a quick summary of the differences:

Feature Standard Mode Monorepo Mode
Multiple projects Separate file system structure Single file system structure
node_modules & package.json Separate instances Shared across monorepo
Default compiler tsc webpack
Compiler settings Specified separately Monorepo defaults that can be overridden per project
Config files like .eslintrc.js, .prettierrc, etc. Specified separately Shared across monorepo
nest build and nest start commands Target defaults automatically to the (only) project in the context Target defaults to the default project in the monorepo
Libraries Managed manually, usually via npm packaging Built-in support, including path management and bundling

Read the sections on Workspaces and Libraries for more detailed information to help you decide which mode is most suitable for you.

CLI 命令语法

All nest commands follow the same format:

nest commandOrAlias requiredArg [optionalArg] [options]

For example:

$ nest new my-nest-project --dry-run

Here, new is the commandOrAlias. The new command has an alias of n. my-nest-project is the requiredArg. If a requiredArg is not supplied on the command line, nest will prompt for it. Also, --dry-run has an equivalent short-hand form -d. With this in mind, the following command is the equivalent of the above:

$ nest n my-nest-project -d

Most commands, and some options, have aliases. Try running nest new --help to see these options and aliases, and to confirm your understanding of the above constructs.

命令概述

Run nest <command> --help for any of the following commands to see command-specific options.

See usage for detailed descriptions for each command.

Command Alias Description
new n 脚手架具有新的*标准模式*应用程序,其中包含运行所需的所有样板文件。
generate g 基于原理图生成和/或修改文件。
build 将应用程序或工作区编译到输出文件夹中。
start 编译并运行应用程序(或工作区中的默认项目)。
add 导入已包装为**nest library**的库,运行其安装示意图。
info i 显示有关已安装的 Nest 软件包和其他有用的系统信息的信息。