Skip to content

NestJs

命令行安装

bash
pnpm i -g @nestjs/cli
bash
yarn global add @nestjs/cli
bash
npm i -g @nestjs/cli

创建新项目

bash
nest new project-name

常用命令

可以使用 nest -h 来查看

namealiasdescription
applicationapplicationGenerate a new application workspace
classclGenerate a new class
configurationconfigGenerate a CLI configuration file
controllercoGenerate a controller declaration
decoratordGenerate a custom decorator
filterfGenerate a filter declaration
gatewaygaGenerate a gateway declaration
guardguGenerate a guard declaration
interceptoritcGenerate an interceptor declaration
interfaceitfGenerate an interface
middlewaremiGenerate a middleware declaration
modulemoGenerate a module declaration
pipepiGenerate a pipe declaration
providerprGenerate a provider declaration
resolverrGenerate a GraphQL resolver declaration
servicesGenerate a service declaration
librarylibGenerate a new library within a monorepo
sub-appappGenerate a new application within a monorepo
resourceresGenerate a new CRUD resource

升级到 10.x 后改动

官方 docs

升级到 10.x 后我们可以使用 swc 启动项目,swc 会使项目启动速度有很大提升。使用 swc 后我们需要做一些改动。

安装 SWC

bash
pnpm i -D @swc/cli @swc/core

项目改动

json
"scripts": {
  "build": "nest build -b swc --type-check",
  "build": "nest build",
  "start:dev": "nest start -b swc --type-check --watch",
  "start:dev": "nest start --watch"
}

nest-cli.json 中添加

json
{
  "$schema": "https://json.schemastore.org/nest-cli",
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "deleteOutDir": true,
    "builder": "swc",
    "typeCheck": true
  }
}