11#! /bin/bash
22
3- # HeadlessX Playwright Setup Script
3+ # HeadlessX Playwright Setup Script (Fixed Version)
44# Installs Playwright and browsers for HeadlessX
55# Run with: bash scripts/setup-playwright.sh
66
4040
4141print_status " Node.js $( node --version) and npm $( npm --version) found"
4242
43+ # Make sure we're in project root
44+ cd " $( dirname " $0 " ) /.."
45+
4346# Install Playwright package
44- echo " 📦 Installing Playwright package..."
47+ echo " 📦 Installing/Updating Playwright package..."
4548if npm install playwright; then
4649 print_status " Playwright package installed"
4750else
5255# Set environment variable to allow browser download
5356export PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=0
5457
55- # Install Chromium browser
58+ # Install browsers using reliable Node.js method
5659echo " 🌐 Installing Chromium browser..."
57- if npx playwright install chromium; then
58- print_status " Chromium browser installed"
59- elif ./node_modules/.bin/playwright install chromium; then
60- print_status " Chromium browser installed (fallback method)"
61- else
62- print_error " Failed to install Chromium browser"
63- exit 1
64- fi
60+ node -e "
61+ const { execSync } = require('child_process');
62+ const path = require('path');
63+
64+ console.log('Attempting to install Chromium browser...');
65+
66+ try {
67+ // Try npx first
68+ execSync('npx playwright install chromium', { stdio: 'inherit' });
69+ console.log('✅ Chromium installed successfully via npx');
70+ } catch (e1) {
71+ console.log('npx method failed, trying alternative...');
72+ try {
73+ // Try node modules bin
74+ const playwrightBin = path.join(process.cwd(), 'node_modules', '.bin', 'playwright');
75+ execSync(\`\"\$ {playwrightBin}\" install chromium\` , { stdio: 'inherit' });
76+ console.log('✅ Chromium installed via direct path');
77+ } catch (e2) {
78+ console.log('Direct path failed, trying to trigger download...');
79+ try {
80+ // Import playwright to trigger browser download
81+ const playwright = require('playwright');
82+ console.log('🌐 Playwright loaded, browser will download on first use');
83+ } catch (e3) {
84+ console.log('❌ All methods failed:', e3.message);
85+ process.exit(1);
86+ }
87+ }
88+ }
89+ "
6590
6691# Install system dependencies for browsers (Linux/Ubuntu)
67- if [[ " $OSTYPE " == " linux-gnu" * ]]; then
92+ if [[ " $OSTYPE " == " linux-gnu" * ]] && command -v apt-get & > /dev/null ; then
6893 echo " 🔧 Installing system dependencies for browsers..."
69- if npx playwright install-deps chromium; then
94+ sudo apt-get update & > /dev/null || true
95+ sudo apt-get install -y \
96+ libnss3 libnspr4 libatk-bridge2.0-0 libdrm2 libxkbcommon0 \
97+ libxcomposite1 libxdamage1 libxrandr2 libgbm1 libxss1 libasound2 \
98+ libatspi2.0-0 libgtk-3-0 & > /dev/null
99+
100+ if [ $? -eq 0 ]; then
70101 print_status " System dependencies installed"
71- elif ./node_modules/.bin/playwright install-deps chromium; then
72- print_status " System dependencies installed (fallback method)"
73102 else
74- print_warning " System dependencies installation failed - browsers may still work"
103+ print_warning " Some system dependencies failed - browsers may still work"
75104 fi
76105fi
77106
78107# Verify installation
79108echo " 🔍 Verifying Playwright installation..."
80- if node -e " const playwright = require('playwright'); console.log('Playwright version:', playwright.chromium.version())" ; then
81- print_status " Playwright is working correctly!"
82- else
83- print_warning " Playwright installation verification failed"
84- fi
109+ node -e "
110+ try {
111+ const playwright = require('playwright');
112+ console.log('✅ Playwright module loads successfully');
113+ console.log('📍 Chromium executable will be downloaded on first launch');
114+ } catch (error) {
115+ console.log('❌ Verification failed:', error.message);
116+ process.exit(1);
117+ }
118+ "
85119
86120echo " "
87121print_status " Playwright setup completed!"
88- echo " 🚀 You can now start HeadlessX with: node src/server.js"
122+ echo " 🚀 You can now start HeadlessX with: node src/server.js"
123+ echo " 📝 Browsers will auto-download on first use if not already present"
0 commit comments