Skip to content

Commit 6ec6ff4

Browse files
committed
try chrome launcher
1 parent 3603d42 commit 6ec6ff4

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

.puppeteerrc.cjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
// We use the system Chrome instead
3+
skipDownload: true,
4+
};

src/run-lighthouse.js

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,52 @@
1-
import puppeteer from 'puppeteer';
1+
import { execSync } from 'child_process';
2+
3+
import chromeLauncher from 'chrome-launcher';
24
import lighthouse from 'lighthouse';
35
import log from 'lighthouse-logger';
4-
import chromeLauncher from 'chrome-launcher';
6+
import puppeteer from 'puppeteer';
57

68
export const getBrowserPath = async () => {
7-
const browser = await puppeteer.launch({
8-
headless: 'new',
9-
args: ['--no-sandbox', '--disable-gpu', '--disable-dev-shm-usage'],
10-
});
11-
const path = browser.process().spawnfile;
12-
await browser.close();
13-
return path;
9+
// First, try to find Chrome using chromeLauncher
10+
// This works well on CI environments like Netlify
11+
try {
12+
const installations = chromeLauncher.getChromePath();
13+
if (installations) {
14+
return installations;
15+
}
16+
} catch (error) {
17+
// chromeLauncher.getChromePath() failed, continue to next method
18+
}
19+
20+
// Try to find Chrome using common paths
21+
try {
22+
const chromePath = execSync(
23+
'which google-chrome || which chromium-browser || which chromium',
24+
{
25+
encoding: 'utf8',
26+
stdio: ['pipe', 'pipe', 'ignore'],
27+
},
28+
).trim();
29+
if (chromePath) {
30+
return chromePath;
31+
}
32+
} catch (error) {
33+
// which command failed, continue to next method
34+
}
35+
36+
// Fall back to using puppeteer's bundled Chrome (if available)
37+
try {
38+
const browser = await puppeteer.launch({
39+
headless: 'new',
40+
args: ['--no-sandbox', '--disable-gpu', '--disable-dev-shm-usage'],
41+
});
42+
const path = browser.process().spawnfile;
43+
await browser.close();
44+
return path;
45+
} catch (error) {
46+
throw new Error(
47+
'Could not find Chrome. Please ensure Chrome is installed on the system.',
48+
);
49+
}
1450
};
1551

1652
export const runLighthouse = async (browserPath, url, settings) => {

0 commit comments

Comments
 (0)