Node.js를 설치하면 Node.js에서 사용하는 패키지를 관리하는 npm(Node Package Manager)도 설치합니다. 즉, npm은 https://www.npmjs.com에 등록된 라이브러리들을 쉽게 설치, 삭제할 수 있게 해줍니다.
NestJS 설치
NestJS는 Node.js를 기반으로 하기 때문에 먼저 Node.js를 설치합니다.
최신 버전은 https://nodejs.org/download 페이지에서 다운로드 후 설치합니다. 설치 방법은 공식 사이트를 참고하세요.
NestJS 설치는 npm을 통해 필요한 라이브러리들을 하나씩 설치하거나 간편하게 프로젝트를 생성할 수 있는 nest-cli 패키지로 설치하는 방법 그리고 git clone으로 작업 디렉토리에 내려받은 후 설정을 변경하는 방법 등이 있습니다.
여기서는 nest-cli 패키지로 설치하는 방법을 알아 보겠습니다.
$ npm i -g @nestjs/cli
...
$ nest -v # nest --version
10.4.9
$ nest
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 --strict helloworld # --strict : typescript
⚡ We will scaffold your app in a few seconds..
? Which package manager would you ❤️ to use? npm
CREATE helloworld/.eslintrc.js (663 bytes)
CREATE helloworld/.prettierrc (51 bytes)
CREATE helloworld/README.md (3340 bytes)
CREATE helloworld/nest-cli.json (171 bytes)
CREATE helloworld/package.json (1951 bytes)
CREATE helloworld/tsconfig.build.json (97 bytes)
CREATE helloworld/tsconfig.json (541 bytes)
CREATE helloworld/src/app.controller.ts (274 bytes)
CREATE helloworld/src/app.module.ts (249 bytes)
CREATE helloworld/src/app.service.ts (142 bytes)
CREATE helloworld/src/main.ts (208 bytes)
CREATE helloworld/src/app.controller.spec.ts (617 bytes)
CREATE helloworld/test/jest-e2e.json (183 bytes)
CREATE helloworld/test/app.e2e-spec.ts (630 bytes)
✔ Installation in progress... ☕
🚀 Successfully created project helloworld
👉 Get started with the following commands:
$ cd helloworld
$ npm run start
Thanks for installing Nest 🙏
Please consider donating to our open collective
to help us maintain this package.
🍷 Donate: https://opencollective.com/nest
프로젝트를 생성하는 단계에서 패키지 매니저를 선택하라는 화면에서 본인이 선호하는 것을 선택합니다. 여기서는 npm을 선택했습니다.
프로젝트에 필요한 패키지가 모두 설치되고 나면 아래와 같은 보일러프레이트(boilerplate) 디렉터리가 생성됩니다.
$ tree
├── README.md
├── nest-cli.json
├── node_modules
├── package-lock.json
├── package.json
├── package-lock.json
├── package.json
├── src
│ ├── app.controller.spec.ts
│ ├── app.controller.ts
│ ├── app.module.ts
│ ├── app.service.ts
│ └── main.ts
├── test
│ ├── app.e2e-spec.ts
│ └── jest-e2e.json
├── tsconfig.build.json
└── tsconfig.json
nest new 로 프로젝트를 설정한 것보다 더 최신 버전의 라이브러리들로 구성하려면 NestJS 공식 예제 스타터 프로젝트를 클론해서 시작할 수 있습니다.
$ git clone https://github.com/nestjs/typescript-starter.git project
$ cd project
$ npm install # or npm i
$ npm run start:dev # 운영시에는 npm run start
[4:14:09 AM] Starting compilation in watch mode...
[4:14:10 AM] Found 0 errors. Watching for file changes.
[Nest] 5179 - 12/28/2024, 4:14:10 AM LOG [NestFactory] Starting Nest application...
[Nest] 5179 - 12/28/2024, 4:14:10 AM LOG [InstanceLoader] AppModule dependencies initialized +5ms
[Nest] 5179 - 12/28/2024, 4:14:10 AM LOG [RoutesResolver] AppController {/}: +6ms
[Nest] 5179 - 12/28/2024, 4:14:10 AM LOG [RouterExplorer] Mapped {/, GET} route +1ms
[Nest] 5179 - 12/28/2024, 4:14:10 AM LOG [NestApplication] Nest application successfully started +3m
참고 사이트
'BACKEND > NestJS' 카테고리의 다른 글
NestJS 알아보기 (0) | 2023.11.25 |
---|