Skip to main content

Nest example

In this guide, you'll learn how to integrate NestJS into moon.

Begin by creating a new NestJS project in the root of an existing moon project (this should not be created in the workspace root, unless a polyrepo).

npx @nestjs/cli@latest new nestjs-app --skip-git

View the official NestJS docs for a more in-depth guide to getting started!

Setup

Since NestJS is per-project, the associated moon tasks should be defined in each project's moon.yml file.

<project>/moon.yml
type: 'application'

fileGroups:
app:
- 'nest-cli.*'

tasks:
dev:
command: 'nest start --watch'
local: true

build:
command: 'nest build'
inputs:
- '@group(app)'
- '@group(sources)'

TypeScript integration

NestJS has built-in support for TypeScript, so there is no need for additional configuration to enable TypeScript support.

At this point we'll assume that a tsconfig.json has been created in the application, and typechecking works. From here we suggest utilizing a global typecheck task for consistency across all projects within the repository.

Configuration

Root-level

We suggest against root-level configuration, as NestJS should be installed per-project, and the nest command expects the configuration to live relative to the project root.

Project-level

When creating a new NestJS project, a nest-cli.json is created, and must exist in the project root. This allows each project to configure NestJS for their needs.

<project>/nest-cli.json
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"type": "application",
"root": "./",
"sourceRoot": "src",
"compilerOptions": {
"tsConfigPath": "tsconfig.build.json"
}
}