Run in a subproject with --cwd
The --cwd flag is one of jpd’s most powerful features for working with complex project structures. It allows you to execute commands in any directory without changing your shell’s current working directory.
Why Use —cwd?
Section titled “Why Use —cwd?”Monorepos
Section titled “Monorepos”Modern JavaScript projects often use monorepo structures with multiple packages:
my-monorepo/├── apps/│ ├── web/ # React frontend│ └── mobile/ # React Native app├── packages/│ ├── ui/ # Shared UI components│ ├── utils/ # Shared utilities│ └── config/ # Shared configuration└── tools/ └── build/ # Build scriptsInstead of constantly using cd to navigate between projects, you can use --cwd to run commands from anywhere:
# From the monorepo root, install deps in all subprojectsjpd install --cwd ./apps/web/jpd install --cwd ./apps/mobile/jpd install --cwd ./packages/ui/jpd install --cwd ./packages/utils/Build Scripts and Automation
Section titled “Build Scripts and Automation”When writing build scripts or CI/CD pipelines, --cwd lets you maintain a single working directory while operating on multiple projects:
#!/bin/bash# build-all.sh - stays in monorepo root
echo "Building all packages..."jpd run --cwd ./packages/ui/ buildjpd run --cwd ./packages/utils/ buildjpd run --cwd ./apps/web/ buildecho "All builds complete!"Development Workflows
Section titled “Development Workflows”During development, you can quickly run commands in related projects without losing your current context:
# Working in the main app, need to test a dependencypwd # /home/user/my-monorepo/apps/web
# Test the shared UI package without changing directoriesjpd run --cwd ./../../packages/ui/ test
pwd # Still /home/user/my-monorepo/apps/webPath Requirements
Section titled “Path Requirements”Valid Path Examples
Section titled “Valid Path Examples”# ✅ Correct - relative paths with trailing slashjpd install --cwd ./my-app/jpd run --cwd ./packages/ui/ buildjpd exec --cwd ./frontend/ create-react-app new-component
# ✅ Correct - absolute paths with trailing slashjpd install --cwd /home/user/projects/my-app/jpd run --cwd /opt/apps/frontend/ dev
# ✅ Correct - root directory (special case, no trailing slash)jpd install --cwd /Invalid Path Examples
Section titled “Invalid Path Examples”# ❌ Wrong - missing trailing slashjpd install --cwd ./my-appjpd run --cwd ./packages/ui build
# ❌ Wrong - these will produce validation errorsjpd exec --cwd /home/user/projects/my-app create-react-appCI Build Flag Behavior
Section titled “CI Build Flag Behavior”In CI environments, path validation can be relaxed by compiling jpd with the CI build flag:
# How to build jpd in CI modego build -ldflags "-X github.com/louiss0/javascript-package-delegator/build_info.rawCI=true" -o jpdWhen compiled in CI mode:
# Only trailing slash paths workjpd install --cwd ./my-app/ # ✅ OKjpd install --cwd ./my-app # ❌ Error# Both formats workjpd install --cwd ./my-app/ # ✅ OKjpd install --cwd ./my-app # ✅ OKReal-World Examples
Section titled “Real-World Examples”Monorepo Package Management
Section titled “Monorepo Package Management”# Install dependencies for all packages from rootjpd install --cwd ./packages/shared/jpd install --cwd ./packages/client/jpd install --cwd ./packages/server/
# Add a dev dependency to the client packagejpd install --cwd ./packages/client/ --dev vitest
# Update dependencies in the server packagejpd update --cwd ./packages/server/Cross-Project Development
Section titled “Cross-Project Development”# Test the impact of changes across multiple projectsjpd run --cwd ./packages/ui/ testjpd run --cwd ./apps/dashboard/ testjpd run --cwd ./apps/mobile/ test
# Build packages in dependency orderjpd run --cwd ./packages/utils/ buildjpd run --cwd ./packages/ui/ build # depends on utilsjpd run --cwd ./apps/web/ build # depends on ui + utilsCI/CD Pipeline Usage
Section titled “CI/CD Pipeline Usage”# GitHub Actions examplesteps: - name: Install dependencies for all packages run: | jpd install --cwd ./packages/core/ jpd install --cwd ./packages/plugins/ jpd install --cwd ./apps/web/
- name: Run tests for all packages run: | jpd run --cwd ./packages/core/ test jpd run --cwd ./packages/plugins/ test jpd run --cwd ./apps/web/ test
- name: Build for production run: | jpd run --cwd ./packages/core/ build jpd run --cwd ./packages/plugins/ build jpd run --cwd ./apps/web/ buildDevelopment Server Management
Section titled “Development Server Management”# Start multiple development servers from a single terminaljpd run --cwd ./apps/api/ dev & # Backend on port 3001jpd run --cwd ./apps/web/ dev & # Frontend on port 3000jpd run --cwd ./apps/admin/ dev & # Admin panel on port 3002
# All servers running, you can monitor logs or stop them as neededwait # Wait for all background processesTroubleshooting
Section titled “Troubleshooting”Common Path Issues
Section titled “Common Path Issues”Problem: “target directory is not a directory”
jpd install --cwd ./nonexistent/# Error: target directory ./nonexistent/ is not a directorySolution: Ensure the directory exists before running the command.
Problem: “no command set to run”
jpd --cwd ./packages/ui/ install # Wrong orderSolution: Global flags must come after the command:
jpd install --cwd ./packages/ui/ # Correct orderPackage Manager Detection
Section titled “Package Manager Detection”When using --cwd, jpd detects the package manager in the target directory, not your current directory:
# Your current directory has package-lock.json (npm)# But ./packages/ui/ has yarn.lock (yarn)
jpd install --cwd ./packages/ui/# jpd will use yarn, not npm, because it looks in ./packages/ui/Relative vs Absolute Paths
Section titled “Relative vs Absolute Paths”# Relative paths are resolved from your current working directorypwd # /home/user/monorepo/apps/webjpd install --cwd ../../packages/ui/ # Goes to /home/user/monorepo/packages/ui/
# Absolute paths work from anywherejpd install --cwd /home/user/monorepo/packages/ui/Advanced Patterns
Section titled “Advanced Patterns”Batch Operations
Section titled “Batch Operations”# Use shell loops for repetitive operationsfor project in apps/*/; do echo "Installing dependencies in $project" jpd install --cwd "./$project"done
# Or with specific projectsprojects=("packages/ui/" "packages/utils/" "apps/web/")for project in "${projects[@]}"; do jpd run --cwd "./$project" testdoneConditional Execution
Section titled “Conditional Execution”# Only run if package.json exists in target directoryif [ -f "./packages/ui/package.json" ]; then jpd install --cwd ./packages/ui/fi
# Run different commands based on detected package managerif jpd agent --cwd ./packages/ui/ | grep -q "yarn"; then echo "Using Yarn in UI package"else echo "Using npm in UI package"fiError Handling
Section titled “Error Handling”# Capture and handle errors appropriatelyif ! jpd install --cwd ./packages/ui/; then echo "Failed to install dependencies in UI package" exit 1fi
# Continue on error for non-critical operationsjpd run --cwd ./packages/ui/ lint || echo "Linting failed, continuing..."See Also
Section titled “See Also”- Complete Commands Reference - Full
--cwddocumentation - Interactive Workflows - Combine
--cwdwith interactive features