网上这个问题解决办法五花八门, 基本上都不管用,

错误代码:

Error: Download failed: server returned code 404. URL: https://registry.npmmirror.com/binary.html/chromium-browser-snapshots/Mac/982053/chrome-mac.zip
    at /xxx/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserFetcher.js:371:27
    at ClientRequest.requestCallback (/xxx/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserFetcher.js:500:13)
    at Object.onceWrapper (node:events:626:26)
    at ClientRequest.emit (node:events:511:28)
    at HTTPParser.parserOnIncomingClient (node:_http_client:693:27)
    at HTTPParser.parserOnHeadersComplete (node:_http_common:119:17)
    at TLSSocket.socketOnData (node:_http_client:535:22)
    at TLSSocket.emit (node:events:511:28)
    at addChunk (node:internal/streams/readable:332:12)
    at readableAddChunk (node:internal/streams/readable:305:9)

直接打开  这个地址  registry.npmmirror.com/binary.html/chromium-browser-snapshots/Mac/982053/chrome-mac.zip  提示文件找不到,很明显这个就是下载地址错误,  注意这里的地址 registry.npmmirror.com/binary.html/  这个一看就不正常,实际的地址应该是  registry.npmmirror.com/-/binary/

解决方法:

PUPPETEER_DOWNLOAD_HOST=https://registry.npmmirror.com/-/binary yarn

即在你的操作命令全增加一个直接的环境变量 PUPPETEER_DOWNLOAD_HOST=https://registry.npmmirror.com/-/binary

后面加个空格,在执行你的yarn 或者npm add xxx 这样就OK了。

下载后的默认存放路径为你当前项目文件夹下的

.local-chromium/mac-982053/chrome-mac/Chromium.app

这里的 982053 是crromium的版本号,注意这个目录默认是隐藏的哦

原因分析

解决这类问题最靠谱的就是查看源码

node_modules/puppeteer/lib/cjs/puppeteer/node/install.js 源码里面的关于puppeteer的下载地址的代码如下:

const downloadHost = process.env.PUPPETEER_DOWNLOAD_HOST ||
        process.env.npm_config_puppeteer_download_host ||
        process.env.npm_package_config_puppeteer_download_host;

这个意思就是会解析 PUPPETEER_DOWNLOAD_HOST 或者  npm_config_puppeteer_download_host  或者 npm_package_config_puppeteer_download_host 这几个系统环境变量获得下载地址的host, 

再看下载地址的拼接源码 node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserFetcher.js 

const downloadURLs = {
    chrome: {
        linux: '%s/chromium-browser-snapshots/Linux_x64/%d/%s.zip',
        mac: '%s/chromium-browser-snapshots/Mac/%d/%s.zip',
        win32: '%s/chromium-browser-snapshots/Win/%d/%s.zip',
        win64: '%s/chromium-browser-snapshots/Win_x64/%d/%s.zip',
    },
    firefox: {
        linux: '%s/firefox-%s.en-US.%s-x86_64.tar.bz2',
        mac: '%s/firefox-%s.en-US.%s.dmg',
        win32: '%s/firefox-%s.en-US.%s.zip',
        win64: '%s/firefox-%s.en-US.%s.zip',
    },
};
const browserConfig = {
    chrome: {
        host: 'https://storage.googleapis.com',
        destination: '.local-chromium',
    },
    firefox: {
        host: 'https://archive.mozilla.org/pub/firefox/nightly/latest-mozilla-central',
        destination: '.local-firefox',
    },
};
function archiveName(product, platform, revision) {
    if (product === 'chrome') {
        if (platform === 'linux')
            return 'chrome-linux';
        if (platform === 'mac')
            return 'chrome-mac';
        if (platform === 'win32' || platform === 'win64') {
            // Windows archive name changed at r591479.
            return parseInt(revision, 10) > 591479 ? 'chrome-win' : 'chrome-win32';
        }
    }
    else if (product === 'firefox') {
        return platform;
    }
}
/**
 * @internal
 */
function downloadURL(product, platform, host, revision) {
    const url = util.format(downloadURLs[product][platform], host, revision, archiveName(product, platform, revision));
    return url;
}

这里最关键的URL拼接代码是最后的这个downloadURL函数,他实际上就是对这个进行替换

util.format('%s/chromium-browser-snapshots/Mac/%d/%s.zip', host, revision, archiveName(product, platform, revision));

而这里的host 就是环境变量中的  PUPPETEER_DOWNLOAD_HOST   , 从上面的拼接代码得知,这个环境变量的host并非我们常见的host,而是要根据你使用的不同的mirror他们存放chromium-browser-snapshots的位置来调整, 且最后不能有 / , 如阿里的就应该是: PUPPETEER_DOWNLOAD_HOST=https://registry.npmmirror.com/-/binary 

效果图

GitHub 加速计划 / ar / Aria
1
3
下载
下载可以很简单
最近提交(Master分支:4 个月前 )
b0d3c6dd - 6 个月前
8fd9634d - 6 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐