Skip to content

Instantly share code, notes, and snippets.

@waiphyo285
Last active December 27, 2025 11:28
Show Gist options
  • Select an option

  • Save waiphyo285/a4199d64029bd35dfb66169fa18645fe to your computer and use it in GitHub Desktop.

Select an option

Save waiphyo285/a4199d64029bd35dfb66169fa18645fe to your computer and use it in GitHub Desktop.

Nx NestJS Workspace Setup - Complete Guide

Prerequisites

  • Node.js (v20 or higher)
  • pnpm installed globally (npm install -g pnpm)

Initial Setup

1. Create Workspace

npx create-nx-workspace@latest nestjs-migrate-nx --preset=ts --packageManager=pnpm
cd nestjs-migrate-nx

2. Install Essential Plugins

pnpm install -D @nx/nest @nx/node @nx/eslint @nx/jest @nx/webpack

Library Creation

3. Create Domain Libraries

# Framework library (infrastructure)
nx g @nx/nest:lib --name=framework --directory=libs/framework --buildable

# Core domain library (core logic)
nx g @nx/nest:lib --name=core --directory=libs/core --buildable

# Plugins library (extensibility)
nx g @nx/nest:lib --name=plugins --directory=libs/plugins --buildable

# Shared library (utilities)
nx g @nx/nest:lib --name=shared --directory=libs/shared --buildable

4. Create Applications

# Admin application
nx g @nx/nest:app --name=admin --directory=apps/admin

# API backend application
nx g @nx/nest:app --name=api --directory=apps/api

# Workers application (background jobs)
nx g @nx/nest:app --name=workers --directory=apps/workers

Build and Serve

5. Build All Projects

nx run-many --target=build --all

6. Serve Applications

# Serve individual applications
nx serve api        # http://localhost:3001
nx serve admin      # http://localhost:3000
nx serve workers    # Background process

# Or serve all applications in development mode
nx run-many --target=serve --all

Essential Nx Commands

Development

# Serve with watch mode
nx serve api --watch

# Serve with different port
nx serve api --port=4000

# Serve with production configuration
nx serve api --configuration=production

Testing

# Test specific project
nx test api
nx test core

# Test all projects
nx run-many --target=test --all

# Test with watch mode
nx test api --watch

Linting

# Lint specific project
nx lint api

# Lint all projects
nx run-many --target=lint --all

# Fix lint issues automatically
nx lint api --fix

Building

# Build specific project
nx build api
nx build core

# Build for production
nx build api --configuration=production

# Build all projects
nx run-many --target=build --all

Visualization

# Show project graph
nx graph

# Show project information
nx show project api

# List all projects
nx show projects

Dependency Management

Adding Dependencies

# Add production dependency to specific project
nx g @nx/node:setup-build --project=api
pnpm add class-validator

# Add dev dependency to workspace
pnpm add -D @types/node

# Add dependency to specific library
pnpm add lodash --filter @nestjs-migrate-nx/core

Dependency Graph

Set up dependencies in project.json files:

{
  "dependencies": {
    "@nestjs-migrate-nx/shared": "*",
    "@nestjs-migrate-nx/core": "*"
  }
}

Configurations

Development Environment

export const environment = {
  production: false,
  databaseUrl: 'postgresql://localhost:5432/dev_db',
  port: 3000,
};

Production Environment

export const environment = {
  production: true,
  databaseUrl: process.env.DATABASE_URL,
  port: process.env.PORT || 3000,
};

Issues & Solutions

Port Already in Use

# Kill process on port 3000
npx kill-port 3000

# Or use different port
nx serve api --port=3001

Build Failures

# Clear Nx cache
nx reset

# Delete node_modules and reinstall
rm -rf node_modules
pnpm install

Module Resolution

# Check dependency graph
nx graph

# Verify imports use correct paths
# Use: @nestjs-migrate-nx/core instead of relative paths

Next Steps (optional)

Installing Packages

pnpm add @nestjs/typeorm typeorm pg
pnpm add @nestjs/passport @nestjs/jwt passport-jwt
pnpm add @nestjs/swagger swagger-ui-express
pnpm add nest-winston winston
pnpm add @nestjs/config

Useful Scripts

{
  "scripts": {
    "dev:api": "nx serve api",
    "dev:admin": "nx serve admin",
    "dev:all": "nx run-many --target=serve --all",
    "build:all": "nx run-many --target=build --all",
    "test:all": "nx run-many --target=test --all",
    "lint:all": "nx run-many --target=lint --all",
    "graph": "nx graph"
  }
}

Access Points

Support

This setup creates a scalable enterprise-grade NestJS monorepo with proper separation of concerns and buildable libraries.

@Jesseekoh
Copy link

Do you have any suggestions on how I'd go about making nx use swc instead of webpack?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment