# Troubleshooting Guide

This guide provides solutions to common issues you might encounter when using the Rapid Startup Development Toolkit. If you're experiencing a problem not covered in this guide, please check the [GitHub repository](https://github.com/rapid-startup-toolkit) for known issues or to report a new one.

## Table of Contents

1. [Installation Issues](#installation-issues)
2. [Project Creation Issues](#project-creation-issues)
3. [Development Issues](#development-issues)
4. [Build Issues](#build-issues)
5. [Deployment Issues](#deployment-issues)
6. [CI/CD Issues](#cicd-issues)
7. [Common Error Messages](#common-error-messages)
8. [Performance Issues](#performance-issues)
9. [Compatibility Issues](#compatibility-issues)
10. [Getting Help](#getting-help)

## Installation Issues

### Toolkit Not Found

**Problem**: After installation, the `rst` command is not recognized.

**Solution**:

1. Ensure the toolkit is installed globally:
   ```bash
   npm install -g rapid-startup-toolkit
   ```

2. Check if the npm global bin directory is in your PATH:
   ```bash
   echo $PATH
   ```

3. If not, add it to your PATH:
   ```bash
   # For bash/zsh
   export PATH="$PATH:$(npm config get prefix)/bin"
   
   # For Windows Command Prompt
   set PATH=%PATH%;%APPDATA%\npm
   
   # For Windows PowerShell
   $env:PATH += ";$env:APPDATA\npm"
   ```

4. Alternatively, use npx to run the toolkit without installing it globally:
   ```bash
   npx rapid-startup-toolkit <command>
   ```

### Permission Errors

**Problem**: You encounter permission errors when installing the toolkit.

**Solution**:

1. On Unix-based systems, you can use sudo (not recommended for security reasons):
   ```bash
   sudo npm install -g rapid-startup-toolkit
   ```

2. A better approach is to fix npm permissions:
   ```bash
   mkdir ~/.npm-global
   npm config set prefix '~/.npm-global'
   export PATH=~/.npm-global/bin:$PATH
   ```
   Add the export line to your `.bashrc` or `.zshrc` file to make it permanent.

3. On Windows, run Command Prompt or PowerShell as Administrator.

### Version Conflicts

**Problem**: You encounter version conflicts with other packages.

**Solution**:

1. Check for conflicting packages:
   ```bash
   npm ls -g --depth=0
   ```

2. Try installing a specific version of the toolkit:
   ```bash
   npm install -g rapid-startup-toolkit@<version>
   ```

3. Use npx to run a specific version:
   ```bash
   npx rapid-startup-toolkit@<version> <command>
   ```

## Project Creation Issues

### Template Not Found

**Problem**: You get an error when specifying a template that doesn't exist.

**Solution**:

1. Check available templates:
   ```bash
   rst create --help
   ```

2. Use a valid template name:
   ```bash
   rst create my-project --template saas
   ```

3. If you need a custom template, create it first or use a local template:
   ```bash
   rst create my-project --template ./my-custom-template
   ```

### Dependency Installation Fails

**Problem**: Dependency installation fails during project creation.

**Solution**:

1. Skip the installation during creation:
   ```bash
   rst create my-project --skip-install
   ```

2. Navigate to the project directory and install dependencies manually:
   ```bash
   cd my-project
   npm install
   ```

3. Check for specific error messages and resolve them:
   - Network issues: Check your internet connection
   - Permission issues: Fix npm permissions
   - Compatibility issues: Check Node.js and npm versions

### Git Initialization Fails

**Problem**: Git initialization fails during project creation.

**Solution**:

1. Skip git initialization during creation:
   ```bash
   rst create my-project --skip-git
   ```

2. Initialize git manually:
   ```bash
   cd my-project
   git init
   git add .
   git commit -m "Initial commit"
   ```

3. Check if git is installed and configured:
   ```bash
   git --version
   git config --list
   ```

## Development Issues

### Development Server Won't Start

**Problem**: The development server fails to start.

**Solution**:

1. Check for port conflicts:
   ```bash
   # On Unix-based systems
   lsof -i :3000
   
   # On Windows
   netstat -ano | findstr :3000
   ```

2. Use a different port:
   ```bash
   rst dev --port 3001
   ```

3. Check for syntax errors in your code:
   ```bash
   npm run lint
   ```

4. Check for environment variable issues:
   - Ensure `.env.local` file exists
   - Verify environment variables are correctly formatted

### Hot Reloading Not Working

**Problem**: Changes to your code are not reflected in the browser.

**Solution**:

1. Ensure you're using the toolkit's dev command:
   ```bash
   rst dev
   ```

2. Check if the file you're editing is being watched:
   - Files outside the `src` directory might not be watched
   - Some file types might be excluded from watching

3. Try restarting the development server:
   ```bash
   # Press Ctrl+C to stop the server
   rst dev
   ```

4. Check for errors in the console:
   - Browser console
   - Terminal where the dev server is running

### Component Generation Issues

**Problem**: Component generation fails or generates incorrect code.

**Solution**:

1. Check the component name and type:
   ```bash
   rst generate component MyComponent --type ui
   ```

2. Ensure the destination directory exists:
   ```bash
   mkdir -p src/components/ui
   rst generate component MyComponent --directory src/components/ui
   ```

3. Check for custom template issues:
   - Verify the template file exists
   - Check for syntax errors in the template

4. Try using interactive mode for more guidance:
   ```bash
   rst generate component MyComponent --interactive
   ```

## Build Issues

### Build Fails

**Problem**: The build process fails.

**Solution**:

1. Check the error message for specific issues:
   - Syntax errors
   - Type errors
   - Missing dependencies

2. Fix TypeScript errors:
   ```bash
   npm run type-check
   ```

3. Fix ESLint errors:
   ```bash
   npm run lint
   ```

4. Try building without linting:
   ```bash
   rst build --no-lint
   ```

5. Check for environment variable issues:
   - Ensure required environment variables are set
   - Verify environment variables are correctly formatted

### Large Bundle Size

**Problem**: The built bundle is too large.

**Solution**:

1. Analyze the bundle:
   ```bash
   rst build --analyze
   ```

2. Identify large dependencies and consider alternatives:
   - Use smaller libraries
   - Use tree-shakable libraries
   - Remove unused dependencies

3. Implement code splitting:
   - Use dynamic imports
   - Use Next.js page-based code splitting
   - Split large components into smaller ones

4. Optimize images and assets:
   - Use appropriate image formats
   - Compress images
   - Lazy load images and assets

### Missing Assets

**Problem**: Assets are missing in the build output.

**Solution**:

1. Ensure assets are in the correct directory:
   - Static assets should be in the `public` directory
   - Imported assets should be in the `src` directory

2. Check import paths:
   - Use relative paths for imports
   - Use absolute paths from the project root

3. Verify the build configuration:
   - Check `next.config.js` for asset handling
   - Ensure assets are not excluded in the build process

## Deployment Issues

### Deployment Fails

**Problem**: The deployment process fails.

**Solution**:

1. Check the error message for specific issues:
   - Build errors
   - Authentication errors
   - Configuration errors

2. Ensure you have the necessary credentials:
   ```bash
   rst deploy:config --interactive
   ```

3. Try deploying with verbose output:
   ```bash
   DEBUG=* rst deploy
   ```

4. Check platform-specific issues:
   - Vercel: Ensure you have the correct team and project settings
   - Netlify: Check site settings and build configuration
   - AWS: Verify AWS credentials and permissions

### Custom Domain Not Working

**Problem**: Custom domain configuration fails.

**Solution**:

1. Ensure DNS records are properly set up:
   - A record pointing to the deployment platform's IP
   - CNAME record pointing to the deployment platform's domain

2. Check if the domain is verified with your deployment platform:
   - Vercel: Add domain in the Vercel dashboard
   - Netlify: Add domain in the Netlify dashboard
   - AWS: Configure Route 53 or other DNS provider

3. Try configuring the domain manually through the platform's dashboard.

4. Wait for DNS propagation (can take up to 48 hours).

### Environment Variables Not Working

**Problem**: Environment variables are not available in the deployed application.

**Solution**:

1. Ensure environment variables are properly set in the deployment platform:
   - Vercel: Set environment variables in the Vercel dashboard
   - Netlify: Set environment variables in the Netlify dashboard
   - AWS: Set environment variables in the AWS console

2. Check if environment variables are being used correctly in your code:
   - Use `process.env.VARIABLE_NAME` in Node.js
   - Use `process.env.NEXT_PUBLIC_VARIABLE_NAME` for client-side code

3. Redeploy the application after setting environment variables:
   ```bash
   rst deploy
   ```

## CI/CD Issues

### CI/CD Pipeline Fails

**Problem**: The CI/CD pipeline fails.

**Solution**:

1. Check the error message in the CI/CD logs:
   - Build errors
   - Test failures
   - Deployment errors

2. Ensure all environment variables are properly set in the CI/CD platform:
   - GitHub Actions: Set secrets in the repository settings
   - GitLab CI: Set variables in the CI/CD settings
   - CircleCI: Set environment variables in the project settings

3. Verify that the CI/CD configuration is correct:
   ```bash
   rst cicd:init --interactive
   ```

4. Test the build locally before pushing:
   ```bash
   npm run build
   npm run test
   ```

### Secrets Not Available

**Problem**: Environment variables or secrets are not available in the CI/CD pipeline.

**Solution**:

1. Ensure secrets are properly configured in your CI/CD platform:
   - GitHub Actions: Set secrets in the repository settings
   - GitLab CI: Set variables in the CI/CD settings
   - CircleCI: Set environment variables in the project settings

2. Check the syntax for accessing secrets in your CI/CD configuration:
   - GitHub Actions: `${{ secrets.SECRET_NAME }}`
   - GitLab CI: `$SECRET_NAME`
   - CircleCI: `$SECRET_NAME`

3. Verify that the secrets have the correct names and values.

### Automatic Deployment Not Working

**Problem**: Automatic deployment is not triggered.

**Solution**:

1. Check if the CI/CD pipeline is running:
   - GitHub Actions: Check the Actions tab
   - GitLab CI: Check the CI/CD pipelines
   - CircleCI: Check the Pipelines dashboard

2. Verify that the deployment step is configured correctly:
   - Check the deployment command
   - Ensure deployment credentials are available
   - Verify deployment conditions (e.g., only on main branch)

3. Check if the deployment platform is configured for CI/CD:
   - Vercel: Link to GitHub/GitLab repository
   - Netlify: Set up continuous deployment
   - AWS: Configure AWS credentials in CI/CD

## Common Error Messages

### "Cannot find module"

**Problem**: Node.js cannot find a required module.

**Solution**:

1. Install the missing dependency:
   ```bash
   npm install <module-name>
   ```

2. Check for typos in the import statement:
   ```javascript
   // Correct
   import { Button } from '@/components/ui/button';
   
   // Incorrect
   import { Button } from '@/components/ui/buttons';
   ```

3. Ensure the module is listed in your package.json:
   ```bash
   npm list <module-name>
   ```

4. Check the module path:
   - Use correct path separators (`/` not `\`)
   - Use correct case sensitivity
   - Use correct file extensions

### "Type error"

**Problem**: TypeScript type checking fails.

**Solution**:

1. Check the error message for the specific type issue:
   - Missing properties
   - Incompatible types
   - Null or undefined values

2. Fix the type definitions or add proper type annotations:
   ```typescript
   // Add type annotations
   const user: User = { name: 'John', age: 30 };
   
   // Use type assertions when necessary
   const element = document.getElementById('app') as HTMLElement;
   
   // Use optional chaining for potentially undefined values
   const name = user?.name;
   ```

3. Use the correct TypeScript interfaces or types:
   ```typescript
   // Define interfaces
   interface User {
     name: string;
     age: number;
   }
   
   // Use utility types
   type PartialUser = Partial<User>;
   ```

### "API route not found"

**Problem**: An API route is not accessible.

**Solution**:

1. Ensure the API route is properly defined:
   ```bash
   rst generate api users
   ```

2. Check the file structure and naming conventions:
   - API routes should be in the `src/app/api` directory
   - File name should match the route name

3. Verify that the HTTP method is correct:
   ```javascript
   // In the API route file
   export async function GET(request) {
     // Handle GET request
   }
   
   export async function POST(request) {
     // Handle POST request
   }
   ```

4. Check for any middleware that might be blocking the request:
   - Authentication middleware
   - CORS middleware
   - Rate limiting middleware

### "Module not found: Error: Can't resolve"

**Problem**: Webpack cannot resolve a module during build.

**Solution**:

1. Check if the module is installed:
   ```bash
   npm install <module-name>
   ```

2. Verify the import path:
   ```javascript
   // Correct
   import { Button } from '@/components/ui/button';
   
   // Incorrect
   import { Button } from '@components/ui/button';
   ```

3. Check the module resolution in webpack configuration:
   - Ensure aliases are correctly configured
   - Verify module resolution paths

4. Try clearing the cache and rebuilding:
   ```bash
   rm -rf .next
   npm run build
   ```

## Performance Issues

### Slow Development Server

**Problem**: The development server is slow to start or respond.

**Solution**:

1. Check for large dependencies or files:
   - Large node_modules directory
   - Large image or asset files
   - Many components or pages

2. Optimize your code:
   - Reduce the number of imports
   - Use dynamic imports for large components
   - Optimize image and asset loading

3. Increase system resources:
   - Close unused applications
   - Increase available memory
   - Use a faster disk

4. Use production mode for testing performance:
   ```bash
   npm run build
   npm run start
   ```

### Slow Build Times

**Problem**: Building the application takes a long time.

**Solution**:

1. Analyze the build process:
   ```bash
   rst build --analyze
   ```

2. Optimize your code:
   - Reduce the number of dependencies
   - Use tree-shakable libraries
   - Remove unused code and imports

3. Use incremental builds:
   - Enable TypeScript incremental compilation
   - Use Next.js incremental static regeneration

4. Optimize the build configuration:
   - Adjust webpack configuration
   - Use parallel processing
   - Optimize asset processing

### Slow Page Load Times

**Problem**: Pages take a long time to load in production.

**Solution**:

1. Analyze the page performance:
   - Use Lighthouse in Chrome DevTools
   - Use the Network tab to identify slow resources

2. Optimize assets:
   - Compress images
   - Use appropriate image formats
   - Lazy load images and assets

3. Implement code splitting:
   - Use dynamic imports
   - Use Next.js page-based code splitting
   - Split large components into smaller ones

4. Optimize server response times:
   - Use caching
   - Optimize API responses
   - Use CDN for static assets

## Compatibility Issues

### Node.js Version Compatibility

**Problem**: The toolkit or project doesn't work with your Node.js version.

**Solution**:

1. Check the required Node.js version:
   ```bash
   npm info rapid-startup-toolkit engines
   ```

2. Install the recommended Node.js version:
   - Use nvm (Node Version Manager) to manage multiple Node.js versions
   - Install the specific version required

3. Use a Node.js version manager:
   ```bash
   # Install nvm
   curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
   
   # Install and use the required Node.js version
   nvm install 16
   nvm use 16
   ```

### Browser Compatibility

**Problem**: The application doesn't work correctly in certain browsers.

**Solution**:

1. Check browser compatibility:
   - Use browser developer tools to identify issues
   - Test in multiple browsers

2. Add polyfills for older browsers:
   - Use @babel/preset-env
   - Add specific polyfills for missing features

3. Use browser-compatible code:
   - Avoid using cutting-edge features without fallbacks
   - Test in target browsers

4. Add browser compatibility testing to your CI/CD pipeline:
   - Use services like BrowserStack or Sauce Labs
   - Implement automated cross-browser testing

### Package Compatibility

**Problem**: Packages have compatibility issues with each other.

**Solution**:

1. Check package versions:
   ```bash
   npm ls
   ```

2. Update packages to compatible versions:
   ```bash
   npm update
   ```

3. Install specific versions of packages:
   ```bash
   npm install package-name@version
   ```

4. Use package resolution in package.json:
   ```json
   {
     "resolutions": {
       "package-name": "version"
     }
   }
   ```

## Getting Help

If you encounter an issue that is not covered in this troubleshooting guide, you can:

1. **Check the Documentation**: The toolkit documentation may have information about your issue.

2. **Search for Known Issues**: Check the GitHub repository for known issues or to report a new one.

3. **Ask the Community**: Join the community Discord server or forum to ask for help.

4. **Report a Bug**: If you believe you've found a bug, report it on the GitHub repository with detailed information:
   - Steps to reproduce
   - Expected behavior
   - Actual behavior
   - Error messages
   - Environment information (OS, Node.js version, npm version)

5. **Request a Feature**: If you have an idea for a new feature or improvement, submit a feature request on the GitHub repository.

6. **Contribute**: If you know how to fix an issue, consider contributing to the toolkit by submitting a pull request.

