Skip to main content

nest-cli

NestJS CLI 是一個命令行工具,讓開發者可以輕鬆創建、管理和開發 NestJS 應用程式。以下是其基本使用方法:

安裝 NestJS CLI

  1. 安裝 NestJS CLI:

    你可以使用 npm(Node.js 套件管理器)來安裝 NestJS CLI。在終端機中輸入以下的指令:

    npm i -g @nestjs/cli
  2. 創建一個新的 NestJS 應用程式:

    使用以下命令創建一個新的 NestJS 應用程式,其中 my-nest-app 是你的應用程式名稱:

    nest new my-nest-app

    程式會提示你選擇一個套件管理器(例如 npm 或 yarn),然後 CLI 會自動安裝所有必要的依賴並創建一個新的 NestJS 應用程式。

  3. 運行 NestJS 應用程式:

    切換到你的應用程式目錄,然後使用以下命令運行你的應用程式:

    cd my-nest-app
    npm run start

    你的應用程式現在應該在 http://localhost:3000 運行。

  4. 創建模組、控制器和服務:

通用的使用方式

 # generate|g [options] <schematic> [name] [path]  Generate a Nest element.
nest g [options] <schematic> [name] [path]
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
librarylibGenerate a new library within a monorepo
middlewaremiGenerate a middleware declaration
modulemoGenerate a module declaration
pipepiGenerate a pipe declaration
providerprGenerate a provider declaration
resolverrGenerate a GraphQL resolver declaration
resourceresGenerate a new CRUD resource
servicesGenerate a service declaration
sub-appappGenerate a new application within a monorepo

做用 resource 的時候可以直接建立 CRUD 的 file 出來 CRUD generator | NestJS - A progressive Node.js framework

IDE 套件管理器

  • 安裝 NestJS Snippets 套件
SnippetPrefixPurpose
Nest Controllern-controllerNestJS Controller
Nest v4 Middlewaren-v4-middlewareNestJS Middleware Version 4
Nest v4 Modulen-v4-moduleNestJS Module v4
Nest Root Modulen-module-rootNestJS Root Module
Nest NestModulen-module-nestNestJS Module Implementing NestModule
Nest Mongoose Servicen-mongoose-serviceNestJS Service for Mongoose
Nest Mongoose Interfacen-mongoose-interfaceNestJS Interface for mongoose
Nest Unit Testn-testNestJS Unit Test
Nest Service Unit Testn-test-serviceNestJS Service Unit Test
Nest Sequelize Entityn-sequelize-entityNestJS Sequelize Entity
Nest Sequelize Providern-sequelize-providerNestJS Sequelize Provider
Nest Sequelize Database Providern-sequelize-database-providerNestJS Sequelize Provider
Nest Interceptorn-interceptorNestJS Interceptor
Nest Guardn-guardNestJS Guard
Nest Providern-providerNestJS Provider
Nest Modulen-moduleNestJS Module
Nest Global Modulen-module-globalNestJS Global Module
Nest Middlewaren-middlewareNestJS Middleware
Nest Pipen-pipeNestJS Pipe
Nest TypeORM Entityn-typeorm-entityNestJS TypeORM Entity
Nest TypeORM Repositoryn-typeorm-repositoryNestJS TypeORM Repository