使用自定义配置时Lighthouse报错“No artifacts were defined”的解决方法
问题:Lighthouse运行报错"No artifacts were defined on the config"
问题复现
使用以下Node.js代码运行Lighthouse时触发报错:
import lighthouse from 'lighthouse'; import * as chromeLauncher from 'chrome-launcher'; const launchChromeAndRunLighthouse = async (url) => { const chrome = await chromeLauncher.launch(); const options = { port: chrome.port }; const runnerResult = await lighthouse(url, options, { settings: { extends: 'lighthouse:default', screenEmulation: { disabled: true, }, extraHeaders: process.argv[2] === 'true' ? { 'k8scluster': 'true', } : {}, artifacts: {}, // 问题所在行 } }); await chrome.kill(); return runnerResult.lhr; }; const NUM_RUNS = process.argv[3] ? parseInt(process.argv[3]) : 10; const targetURL = process.argv[4] || 'https://www.google.com/'; const runLighthouseMultipleTimes = async (url, numRuns) => { const runs = Array.from({ length: numRuns }, async () => { const result = await launchChromeAndRunLighthouse(url); return result; }); return Promise.all(runs); }; runLighthouseMultipleTimes(targetURL, NUM_RUNS) .then((results) => { const avgPerformanceScore = results.reduce((sum, result) => sum + result.categories.performance.score, 0) / NUM_RUNS; const avgAccessibilityScore = results.reduce((sum, result) => sum + result.categories.accessibility.score, 0) / NUM_RUNS; const avgBestPracticesScore = results.reduce((sum, result) => sum + result.categories['best-practices'].score, 0) / NUM_RUNS; const avgSEOscore = results.reduce((sum, result) => sum + result.categories.seo.score, 0) / NUM_RUNS; console.log(results); console.log(`Average Performance Score: ${avgPerformanceScore}`); console.log(`Average Accessibility Score: ${avgAccessibilityScore}`); console.log(`Average Best Practices Score: ${avgBestPracticesScore}`); console.log(`Average SEO Score: ${avgSEOscore}`); }) .catch((err) => { console.error('Error running Lighthouse:', err); });
报错信息:
Error running Lighthouse: Error: No artifacts were defined on the config
该报错来自lighthouse/core/gather/navigation-runner.js中的检查逻辑:
async function _navigations(args) { const { driver, page, resolvedConfig, requestor, baseArtifacts, computedCache, } = args; if (!resolvedConfig.artifacts || !resolvedConfig.navigations) { throw new Error('No artifacts were defined on the config'); } // 后续逻辑... }
问题原因
你在自定义配置的settings中手动添加了artifacts: {},这会直接覆盖Lighthouse默认配置(lighthouse:default)中定义的完整artifacts集合。当导航运行器检查resolvedConfig.artifacts时,发现其为空对象,触发了报错逻辑。
解决方法
删除配置中的artifacts: {}行即可,默认配置已经包含了Lighthouse运行所需的所有artifacts定义。修改后的配置片段如下:
const runnerResult = await lighthouse(url, options, { settings: { extends: 'lighthouse:default', screenEmulation: { disabled: true, }, extraHeaders: process.argv[2] === 'true' ? { 'k8scluster': 'true', } : {}, // 移除artifacts: {}这一行 } });
内容的提问来源于stack exchange,提问作者Aero Wang
相关产品推荐
相关产品推荐

