Skip to content

Commit a1a6d4d

Browse files
committed
try chrome launcher
1 parent 3603d42 commit a1a6d4d

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-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: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,49 @@
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('which google-chrome || which chromium-browser || which chromium', {
23+
encoding: 'utf8',
24+
stdio: ['pipe', 'pipe', 'ignore'],
25+
}).trim();
26+
if (chromePath) {
27+
return chromePath;
28+
}
29+
} catch (error) {
30+
// which command failed, continue to next method
31+
}
32+
33+
// Fall back to using puppeteer's bundled Chrome (if available)
34+
try {
35+
const browser = await puppeteer.launch({
36+
headless: 'new',
37+
args: ['--no-sandbox', '--disable-gpu', '--disable-dev-shm-usage'],
38+
});
39+
const path = browser.process().spawnfile;
40+
await browser.close();
41+
return path;
42+
} catch (error) {
43+
throw new Error(
44+
'Could not find Chrome. Please ensure Chrome is installed on the system.',
45+
);
46+
}
1447
};
1548

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

0 commit comments

Comments
 (0)