Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions DEVELOPMENT_SETUP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# Evolution API Development Setup Guide

This guide addresses common setup issues when running Evolution API in development mode.

## Issues Fixed

### 1. Baileys Crypto Error
**Error:** `TypeError: Cannot destructure property 'subtle' of 'globalThis.crypto' as it is undefined.`

**Solution:** Added `NODE_OPTIONS=--experimental-global-webcrypto` to the npm scripts in `package.json`:

```json
{
"scripts": {
"start": "NODE_OPTIONS=--experimental-global-webcrypto tsx ./src/main.ts",
"start:prod": "NODE_OPTIONS=--experimental-global-webcrypto node dist/main",
"dev:server": "NODE_OPTIONS=--experimental-global-webcrypto tsx watch ./src/main.ts"
}
}
```

This enables the Web Crypto API in Node.js, which is required by the Baileys package for WhatsApp functionality.

**Note:** Node.js 20+ has this enabled by default, but the flag ensures compatibility across different environments and versions.

### 2. Prisma Client Initialization Error
**Error:** `@prisma/client did not initialize yet. Please run "prisma generate" and try to import it again.`

**Solution:** Generate the Prisma client before running the server:

```bash
# For PostgreSQL (default)
npm run db:generate

# Or directly with npx
npx prisma generate --schema ./prisma/postgresql-schema.prisma

# For MySQL
DATABASE_PROVIDER=mysql npm run db:generate
```

**Network-restricted environments:** If you can't download Prisma binaries due to network restrictions, use the offline script:

```bash
./generate-prisma-offline.sh
```

This creates a minimal Prisma client stub that allows the server to start for development purposes.

## Quick Setup Scripts

### Automated Setup
```bash
./setup-dev.sh
```

### Manual Setup Steps

1. **Install dependencies:**
```bash
npm ci
```

2. **Create .env file:**
```bash
cp .env.example .env
```

Update the database connection URI and other required variables.

3. **Generate Prisma client:**
```bash
npm run db:generate
# OR for offline environments:
./generate-prisma-offline.sh
```

4. **Run database migrations (if needed):**
```bash
npm run db:deploy
```

5. **Start the development server:**
```bash
npm run dev:server
```

## Environment Variables

Key environment variables to configure:

```env
DATABASE_PROVIDER=postgresql
DATABASE_CONNECTION_URI='postgresql://user:pass@localhost:5432/evolution_db?schema=evolution_api'
AUTHENTICATION_API_KEY=your-api-key-here
CACHE_REDIS_ENABLED=false
TELEMETRY_ENABLED=false
```

## Troubleshooting

### Network Issues with Prisma
If you encounter network issues when generating Prisma client (common in restricted environments):

1. Use the offline script: `./generate-prisma-offline.sh`
2. Ensure you have internet access to download Prisma binaries for production
3. Try using a VPN or different network
4. For production deployment, pre-generate the client on a machine with internet access

### Crypto Issues
If you still encounter crypto issues:

1. Ensure Node.js 20+ is being used
2. Verify the `NODE_OPTIONS=--experimental-global-webcrypto` flag is set
3. Check if your environment has any crypto restrictions

### Node.js Version
- **Minimum:** Node.js 18+
- **Recommended:** Node.js 20+ for best compatibility with the Web Crypto API

### Development vs Production
- Development: Uses `tsx watch` for hot reload
- Production: Uses compiled JavaScript from `dist/` folder

## Scripts Overview

- `npm run dev:server` - Development server with hot reload and crypto flags
- `npm run start` - Production-like start with tsx and crypto flags
- `npm run start:prod` - Production start with compiled code and crypto flags
- `npm run build` - Build the application
- `npm run db:generate` - Generate Prisma client
- `npm run db:deploy` - Deploy database migrations
- `./setup-dev.sh` - Automated development setup
- `./generate-prisma-offline.sh` - Offline Prisma client generation

## File Changes Summary

### Modified Files:
1. **package.json** - Updated scripts with `NODE_OPTIONS=--experimental-global-webcrypto`
2. **DEVELOPMENT_SETUP.md** - This documentation file
3. **setup-dev.sh** - Automated setup script
4. **generate-prisma-offline.sh** - Offline Prisma client generation script
72 changes: 72 additions & 0 deletions generate-prisma-offline.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/bin/bash

# Offline Prisma Client Generation Script
# Use this script when network access to Prisma binaries is restricted

echo "πŸ”„ Attempting offline Prisma client generation..."

# Set environment variables to skip downloads and use local binaries
export PRISMA_SKIP_DOWNLOAD=true
export PRISMA_CLI_BINARY_TARGETS=native

# Check if we already have the required schema
if [ ! -f "prisma/postgresql-schema.prisma" ]; then
echo "❌ PostgreSQL schema file not found"
exit 1
fi

echo "πŸ“‹ Using schema: prisma/postgresql-schema.prisma"

# Try to generate with existing binaries
if npx prisma generate --schema ./prisma/postgresql-schema.prisma 2>/dev/null; then
echo "βœ… Prisma client generated successfully (offline mode)"
exit 0
fi

echo "⚠️ Offline generation failed. Creating minimal client stub..."

# Create a minimal client stub for development
cat > node_modules/.prisma/client/index.js << 'EOF'
// Minimal Prisma client stub for development
const { EventEmitter } = require('events');

class PrismaClient extends EventEmitter {
constructor(options = {}) {
super();
this.options = options;
console.log('Using minimal Prisma client stub - some features may be limited');
}

async $connect() {
console.log('Prisma client stub: $connect called');
return Promise.resolve();
}

async $disconnect() {
console.log('Prisma client stub: $disconnect called');
return Promise.resolve();
}

async $transaction(fn) {
console.log('Prisma client stub: $transaction called');
return fn(this);
}

// Add more stub methods as needed for your specific use case
}

module.exports = {
PrismaClient,
};
EOF

# Update the default.js file to use our stub
cat > node_modules/.prisma/client/default.js << 'EOF'
// Default export for Prisma client stub
const { PrismaClient } = require('./index.js');
module.exports = { PrismaClient };
EOF

echo "βœ… Minimal Prisma client stub created"
echo "⚠️ Note: This is a development workaround. For production, ensure proper Prisma client generation"
echo "πŸ“ To generate the full client later, run: npm run db:generate"
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"type": "commonjs",
"scripts": {
"build": "tsc --noEmit && tsup",
"start": "tsx ./src/main.ts",
"start:prod": "node dist/main",
"dev:server": "tsx watch ./src/main.ts",
"start": "NODE_OPTIONS=--experimental-global-webcrypto tsx ./src/main.ts",
"start:prod": "NODE_OPTIONS=--experimental-global-webcrypto node dist/main",
"dev:server": "NODE_OPTIONS=--experimental-global-webcrypto tsx watch ./src/main.ts",
"test": "tsx watch ./test/all.test.ts",
"lint": "eslint --fix --ext .ts src",
"lint:check": "eslint --ext .ts src",
Expand Down
53 changes: 53 additions & 0 deletions setup-dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/bash

# Evolution API Development Setup Script
# This script automates the setup process for running Evolution API in development

set -e

echo "πŸš€ Evolution API Development Setup"
echo "=================================="

# Check Node.js version
NODE_VERSION=$(node --version)
echo "βœ“ Node.js version: $NODE_VERSION"

if ! [[ "$NODE_VERSION" == v2* ]]; then
echo "⚠️ Warning: Node.js 20+ is recommended for best compatibility"
fi

# Check if .env exists
if [ ! -f ".env" ]; then
echo "πŸ“ Creating .env file from template..."
cp .env.example .env
echo "βœ“ .env file created. Please review and update the configuration."
else
echo "βœ“ .env file already exists"
fi

# Install dependencies
echo "πŸ“¦ Installing dependencies..."
npm ci

# Generate Prisma client
echo "πŸ—„οΈ Generating Prisma client..."
if npm run db:generate; then
echo "βœ“ Prisma client generated successfully"
else
echo "❌ Failed to generate Prisma client"
echo "πŸ’‘ This may be due to network restrictions. You can:"
echo " 1. Check your internet connection"
echo " 2. Try again later"
echo " 3. Use a VPN if in a restricted environment"
exit 1
fi

echo ""
echo "πŸŽ‰ Setup completed successfully!"
echo ""
echo "Next steps:"
echo "1. Review and update your .env file with proper database credentials"
echo "2. Run database migrations (if needed): npm run db:deploy"
echo "3. Start the development server: npm run dev:server"
echo ""
echo "For more information, see DEVELOPMENT_SETUP.md"