πŸ”§ DevOps

Nx

A monorepo development tool and build system

πŸ“– Detailed Explanation

Nx is a build system and development tool that efficiently performs builds, tests, and linting in a monorepo environment. It lets ordinary teams easily adopt the monorepo approach used by large companies such as Google and Facebook. Developed by Nrwl, it is currently evolving very actively.

The core features of Nx are Affected Commands, which build/test only the affected projects; Computation Caching, which reuses previous build results; and Distributed Task Execution, which runs local and remote Tasks in parallel. Even with 100 packages, only the 3 changed packages are built, so CI time is dramatically reduced.

It automatically analyzes the dependency graph between projects to determine the correct build order. The `nx graph` command can visualize dependencies, which is also useful for understanding the architecture. It warns you when a circular dependency arises, helping keep the codebase healthy.

Nx supports various frameworks such as React, Angular, Next.js, Node.js, and NestJS, and its plugin system lets you create custom generators and executors. Compared to other monorepo tools such as Turborepo and Lerna, it offers stronger caching and distributed execution capabilities.

πŸ’» Code Example

// nx.json - Nx μ›Œν¬μŠ€νŽ˜μ΄μŠ€ μ„€μ •
{
  "$schema": "./node_modules/nx/schemas/nx-schema.json",
  "namedInputs": {
    "default": ["{projectRoot}/**/*", "sharedGlobals"],
    "production": [
      "default",
      "!{projectRoot}/**/?(*.)+(spec|test).[jt]s?(x)?(.snap)",
      "!{projectRoot}/tsconfig.spec.json"
    ],
    "sharedGlobals": ["{workspaceRoot}/babel.config.json"]
  },
  "targetDefaults": {
    "build": {
      "dependsOn": ["^build"],  // 의쑴 ν”„λ‘œμ νŠΈ λ¨Όμ € λΉŒλ“œ
      "inputs": ["production", "^production"],
      "cache": true
    },
    "test": {
      "inputs": ["default", "^production", "{workspaceRoot}/jest.preset.js"],
      "cache": true
    },
    "lint": {
      "inputs": ["default", "{workspaceRoot}/.eslintrc.json"],
      "cache": true
    }
  },
  "tasksRunnerOptions": {
    "default": {
      "runner": "nx/tasks-runners/default",
      "options": {
        "cacheableOperations": ["build", "lint", "test"],
        "parallel": 3
      }
    }
  },
  // Nx Cloud둜 원격 μΊμ‹œ ν™œμ„±ν™”
  "nxCloudAccessToken": "your-access-token"
}
// apps/web/project.json - ν”„λ‘œμ νŠΈ μ„€μ •
{
  "name": "web",
  "$schema": "../../node_modules/nx/schemas/project-schema.json",
  "sourceRoot": "apps/web/src",
  "projectType": "application",
  "tags": ["scope:web", "type:app"],
  "targets": {
    "build": {
      "executor": "@nx/next:build",
      "outputs": ["{options.outputPath}"],
      "options": {
        "outputPath": "dist/apps/web"
      },
      "configurations": {
        "production": {
          "optimization": true,
          "sourceMap": false
        }
      }
    },
    "serve": {
      "executor": "@nx/next:server",
      "options": {
        "buildTarget": "web:build",
        "dev": true
      }
    }
  },
  "implicitDependencies": ["shared-ui", "api-client"]
}

// 자주 μ‚¬μš©ν•˜λŠ” Nx CLI λͺ…λ Ήμ–΄
// nx run web:build              - νŠΉμ • ν”„λ‘œμ νŠΈ λΉŒλ“œ
// nx affected:build --base=main  - λ³€κ²½λœ ν”„λ‘œμ νŠΈλ§Œ λΉŒλ“œ
// nx run-many -t build -p web,api  - μ—¬λŸ¬ ν”„λ‘œμ νŠΈ λΉŒλ“œ
// nx graph                       - μ˜μ‘΄μ„± κ·Έλž˜ν”„ μ‹œκ°ν™”
// nx reset                       - μΊμ‹œ μ΄ˆκΈ°ν™”

πŸ—£οΈ How It's Said in Practice

Lead: "Since the frontend and backend repos are separate, it's hard to share common type definitions. How about switching to a monorepo with Nx?"

Junior: "Doesn't CI take a long time with a monorepo?"

Senior: "If you use Nx affected, only the changed projects get built. And if you connect Nx Cloud, the cache is shared, so it gets faster both locally and in CI."

Interviewer: "Do you have experience optimizing build times in large-scale projects?"

Candidate: "I introduced Nx in a monorepo of 12 packages. By using the affected command to build only the packages affected by changes, the average PR build time dropped from 15 minutes to 4 minutes, and by sharing a remote cache through Nx Cloud, every team member achieved a cache hit rate of over 80%. I also found and cleaned up circular references using the dependency graph."

Take a mock interview on this topic β†’ WIA SOOM AI Interview Β· Start for free

Reviewer: "Please add scope:shared to this package's tags. That way, the module-boundary rule can block incorrect imports."

Author: "I'll add tags to project.json and also configure the @nx/enforce-module-boundaries rule in .eslintrc."

⚠️ Cautions

πŸ“š Learn More