Back to Moon

Nest example

website/docs/guides/examples/nest.mdx

2.2.42.2 KB
Original Source

import AddDepsTabs from '@site/src/components/AddDepsTabs'; import CreateDepTabs from '@site/src/components/CreateDepTabs'; import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; import HeadingApiLink from '@site/src/components/Docs/HeadingApiLink';

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).

shell
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.* file.

yaml
layer: 'application'

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

tasks:
  dev:
    command: 'nest start --watch'
    preset: 'server'

  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.

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