Skip to content

Commit c20b212

Browse files
committed
fix: improve Playwright installation script with enhanced error handling and verification
1 parent d8d2223 commit c20b212

2 files changed

Lines changed: 105 additions & 44 deletions

File tree

scripts/setup-playwright.sh

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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

@@ -40,8 +40,11 @@ fi
4040

4141
print_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..."
4548
if npm install playwright; then
4649
print_status "Playwright package installed"
4750
else
@@ -52,37 +55,69 @@ fi
5255
# Set environment variable to allow browser download
5356
export PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=0
5457

55-
# Install Chromium browser
58+
# Install browsers using reliable Node.js method
5659
echo "🌐 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
76105
fi
77106

78107
# Verify installation
79108
echo "🔍 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

86120
echo ""
87121
print_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"

scripts/setup.sh

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -215,37 +215,63 @@ else
215215
print_status "NPM dependencies installed"
216216
fi
217217

218-
# Install Playwright browsers (enhanced installation)
218+
# Install Playwright browsers (fixed installation)
219219
echo "🌐 Installing Playwright browsers..."
220220
export PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=0
221221

222-
# First ensure playwright is installed as a dependency
223-
if ! command -v npx &> /dev/null; then
224-
print_error "npx not found. Please ensure Node.js is properly installed."
225-
exit 1
226-
fi
222+
# Make sure we're in project root
223+
cd "$(dirname "$0")/.."
227224

228-
# Install playwright package if not already installed
229-
if ! npm list playwright &> /dev/null; then
230-
echo "📦 Installing Playwright package..."
225+
# Check if playwright is in node_modules and install if needed
226+
if [ ! -d "./node_modules/playwright" ]; then
227+
echo "📦 Playwright not found in node_modules, installing..."
231228
npm install playwright
229+
print_status "Playwright package installed"
230+
else
231+
print_status "Playwright package found"
232232
fi
233233

234-
# Install browsers with error handling
234+
# Install browsers using Node.js directly (most reliable method)
235235
echo "🌐 Installing Chromium browser..."
236-
if npx playwright install chromium; then
237-
print_status "Chromium browser installed"
238-
else
239-
print_warning "Chromium installation failed, trying alternative method..."
240-
./node_modules/.bin/playwright install chromium || print_error "Failed to install Chromium"
236+
node -e "
237+
const { execSync } = require('child_process');
238+
const path = require('path');
239+
240+
try {
241+
// Method 1: Try npx
242+
execSync('npx playwright install chromium', { stdio: 'inherit' });
243+
console.log('✅ Chromium installed via npx');
244+
} catch (e1) {
245+
try {
246+
// Method 2: Try direct path
247+
const playwrightBin = path.join(__dirname, 'node_modules', '.bin', 'playwright');
248+
execSync(\`\"\${playwrightBin}\" install chromium\`, { stdio: 'inherit' });
249+
console.log('✅ Chromium installed via direct path');
250+
} catch (e2) {
251+
try {
252+
// Method 3: Use playwright directly
253+
const playwright = require('playwright');
254+
console.log('🌐 Triggering browser download...');
255+
playwright.chromium.launch().then(browser => browser.close()).catch(() => {});
256+
console.log('✅ Chromium download triggered');
257+
} catch (e3) {
258+
console.log('⚠️ All installation methods failed');
259+
}
260+
}
261+
}
262+
" || print_warning "Chromium installation failed - server will download on first use"
263+
264+
# Install system dependencies for Linux
265+
if [[ \"\$OSTYPE\" == \"linux-gnu\"* ]] && command -v apt-get &> /dev/null; then
266+
echo "🔧 Installing browser system dependencies..."
267+
apt-get update &>/dev/null || true
268+
apt-get install -y \
269+
libnss3 libnspr4 libatk-bridge2.0-0 libdrm2 libxkbcommon0 \
270+
libxcomposite1 libxdamage1 libxrandr2 libgbm1 libxss1 libasound2 \
271+
libatspi2.0-0 libgtk-3-0 &>/dev/null && \
272+
print_status "Browser system dependencies installed" || \
273+
print_warning "Some system dependencies failed - browsers may still work"
241274
fi
242-
243-
# Install browser dependencies
244-
echo "🔧 Installing browser system dependencies..."
245-
if npx playwright install-deps chromium; then
246-
print_status "Browser system dependencies installed"
247-
else
248-
print_warning "System dependencies installation failed - continuing anyway"
249275
fi
250276

251277
print_status "Playwright browsers installation completed"

0 commit comments

Comments
 (0)