Lighthouse is an amazing tool by Google for auditing web pages for performance, accessibility, SEO, and more. Running it via CLI (Command Line Interface) is super handy for automation and CI/CD pipelines. Here's how to generate a report:
@lhci/cli is for advanced features (optional but recommended).
lighthouse is the core tool.
Basic Usage: Generate a Report
Run the following command in your terminal:
bash
lighthouse https://example.com --output html --output-path ./report.html
Here's what the flags do:
https://example.com: Replace with the URL you want to audit.
--output html: Generates an HTML report (other options: json, csv).
--output-path ./report.html: Saves the report to report.html in the current directory.
Example Output:
bash
$ lighthouse https://example.com --output html --output-path ./report.html
Running Lighthouse audit for https://example.com
...
Report generated. Writing HTML report to ./report.html.
Open ./report.html in your browser to view the detailed report.
Advanced Options
1. Choose Audit Categories
Select specific categories to audit (default: all categories):
bash
lighthouse https://example.com \
--output html \
--output-path ./report.html \
--only-categories=performance,accessibility,seo
Categories:
performance
accessibility
best-practices
seo
pwa (Progressive Web App)
2. Mobile or Desktop Simulation
By default, Lighthouse emulates a mobile device. To simulate desktop:
bash
lighthouse https://example.com \
--output html \
--output-path ./report.html \
--emulated-form-factor=desktop
Options:
mobile (default)
desktop
none (disable emulation)
3. Set Network Conditions
Throttle the network to simulate slower connections:
Uploads reports to a public URL (e.g., https://storage.googleapis.com/lighthouse-infrastructure.appspot.com/reports/...).
Tips & Tricks
Bypass Authentication: Use --chrome-flags="--disable-extensions --user-agent='Lighthouse'".
Localhost Testing: Add --port=8080 if your site runs locally on a custom port.
Debug Errors: Run with --verbose for detailed logs.
Now you're all set! Run Lighthouse from the CLI and automate web audits like a pro 🚀. Let me know if you need help with CI setups or parsing JSON reports!
User
but i havent installed it via npm, something else?
Assistant
No worries! If you haven't installed Lighthouse via npm, here are alternative ways to run Lighthouse from the CLI:
Option 1: Use Lighthouse via Chrome Browser (No Installation Required)
Open Chrome (v60+).
Press F12 or Ctrl + Shift + I (Windows/Linux) or Cmd + Opt + I (Mac) to open DevTools.
Go to the Lighthouse tab (might be hidden under >> in the tab bar).
docker run -it --rm --cap-add=SYS_ADMIN \
-v $(pwd):/report \
googlechrome/lighthouse \
https://example.com \
--output html --output-path /report/lighthouse-report.html
What’s happening:
-v $(pwd):/report: Maps your current folder to /report inside Docker.
googlechrome/lighthouse: Official Lighthouse Docker image.
--output html: Generates an HTML report.
--output-path /report/lighthouse-report.html: Saves the report in your current folder.
Example Output:
bash
$ docker run ... googlechrome/lighthouse https://example.com --output html --output-path /report/lighthouse-report.html
Running Lighthouse audit for https://example.com
...
Report generated. Writing HTML report to /report/lighthouse-report.html.
Find lighthouse-report.html in your current directory.
Tips:
For desktop mode, add --emulated-form-factor=desktop.
For JSON output, replace html with json.
Option 4: Install Lighthouse via Homebrew (macOS)
If you're on macOS and prefer Homebrew:
bash
brew install lighthouse
Then run it like this:
bash
lighthouse https://example.com --output html --output-path ./report.html
Option 5: Use npx (No Permanent Install)
Even if you don’t have npm installed globally, you can use npx (Node’s package runner):