puppeteer 启动 lambda 函数时出现executablePath 错误

问题描述 投票:0回答:1

我在 sst 应用程序中遇到了一个问题,我想启动一个 puppeteer lambda 函数

import puppeteer from 'puppeteer-core';
import chromium from '@sparticuz/chromium';

export async function handler() {
  console.log('Launching browser...');
  const browser = await puppeteer.launch({
    args: chromium.args,
    defaultViewport: chromium.defaultViewport,
    executablePath: await chromium.executablePath(),
    headless: chromium.headless,
  });

这些是我安装的依赖项:

  "dependencies": {
    "@aws-cdk/aws-iam": "^1.204.0",
    "@aws-cdk/aws-s3": "^1.204.0",
    "@aws-sdk/client-s3": "^3.540.0",
    "@sparticuz/chromium": "119",
    "aws-sdk": "^2.1590.0",
    "chrome-aws-lambda": "^10.1.0",
    "puppeteer": "^22.6.1",
    "puppeteer-core": "21.5.0"
  }

但是我有错误:输入目录“/var/task/packages/functions/bin”不存在。 在部署中。

aws-lambda puppeteer chromium serverless-framework
1个回答
0
投票

我在 sst 上也面临着同样的问题。我从 https://github.com/Sparticuz/chromium?tab=readme-ov-file 下载了 zip,并将 zip 添加到各层,将其附加到 lambda 函数。但傀儡师不断收到错误: 错误生成收据时出错:错误:输入目录“/var/task/app/stacks/bin”不存在。 我检查了@sparticuz/chromium的源代码:

static async executablePath(input) {
        /**
         * If the `chromium` binary already exists in /tmp/chromium, return it.
         */
        if ((0, node_fs_1.existsSync)("/tmp/chromium") === true) {
            return Promise.resolve("/tmp/chromium");
        }
        /**
         * If input is a valid URL, download and extract the file. It will extract to /tmp/chromium-pack
         * and executablePath will be recursively called on that location, which will then extract
         * the brotli files to the correct locations
         */
        if (input && (0, helper_1.isValidUrl)(input)) {
            return this.executablePath(await (0, helper_1.downloadAndExtract)(input));
        }
        /**
         * If input is defined, use that as the location of the brotli files,
         * otherwise, the default location is ../bin.
         * A custom location is needed for workflows that using custom packaging.
         */
        input ??= (0, node_path_1.join)(__dirname, "..", "bin");
        /**
         * If the input directory doesn't exist, throw an error.
         */
        if (!(0, node_fs_1.existsSync)(input)) {
            throw new Error(`The input directory "${input}" does not exist.`);
        }
        // Extract the required files
        const promises = [
            lambdafs_1.default.inflate(`${input}/chromium.br`),
            lambdafs_1.default.inflate(`${input}/fonts.tar.br`),
        ];
        if (this.graphics) {
            // Only inflate graphics stack if needed
            promises.push(lambdafs_1.default.inflate(`${input}/swiftshader.tar.br`));
        }
        if ((0, helper_1.isRunningInAwsLambda)()) {
            // If running in AWS Lambda, extract more required files
            promises.push(lambdafs_1.default.inflate(`${input}/al2.tar.br`));
        }
        if ((0, helper_1.isRunningInAwsLambdaNode20)()) {
            promises.push(lambdafs_1.default.inflate(`${input}/al2023.tar.br`));
        }
        // Await all extractions
        const result = await Promise.all(promises);
        // Returns the first result of the promise, which is the location of the `chromium` binary
        return result.shift();
    }

然后我尝试打印/tmp和/opt中的所有文件夹,只有node-moudles文件夹,但没有找到与chromium相关的层。由于截止日期临近,我最终放弃了这个解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.