alixaxel / chrome-aws-lambda

Chromium Binary for AWS Lambda and Google Cloud Functions
MIT License
3.17k stars 289 forks source link

[BUG] Error: Could not find expected browser (chrome) locally. Run `npm install` to download the correct Chromium revision (901912). #296

Open kurtsys opened 1 year ago

kurtsys commented 1 year ago

Environment

Expected Behavior

Puppeteer launching when AWS lambda starts.

Current Behavior

Error: AWS lambda doesn't find the (correct) chromium browser.

START RequestId: 50e21a5f-c30b-400e-97d6-a0124de70d1c Version: $LATEST
--
2023-06-23T06:48:02.530Z    50e21a5f-c30b-400e-97d6-a0124de70d1c    INFO    Starting test run with SESSION ID: 6b9070a5-55a0-43e9-b2d3-2d3fb63d3ba9
2023-06-23T06:48:02.530Z    50e21a5f-c30b-400e-97d6-a0124de70d1c    ERROR   An exception occurred during judgement...
2023-06-23T06:48:02.530Z    50e21a5f-c30b-400e-97d6-a0124de70d1c    ERROR   Error: Could not find expected browser (chrome) locally. Run `npm install` to download the correct Chromium revision (901912).    at ChromeLauncher.launch (/var/task/node_modules/puppeteer-core/lib/cjs/puppeteer/node/Launcher.js:88:27)    at async newPage (file:///var/task/index.js:28:19)    at async Runtime.handler (file:///var/task/index.js:123:12)
2023-06-23T06:48:02.530Z    50e21a5f-c30b-400e-97d6-a0124de70d1c    INFO    [...]
2023-06-23T06:48:02.625Z    50e21a5f-c30b-400e-97d6-a0124de70d1c    INFO    Shutting down judge...
2023-06-23T06:48:02.625Z    50e21a5f-c30b-400e-97d6-a0124de70d1c    INFO    Judge is shut down...

The code

package.json:

{
  "name": "@ftrprf/scratch-judge-serverless-api-aws",
  "private": "true",
  "version": "0.0.1",
  "type": "module",
  "scripts": {
    "start": "node src/index.js",
    "clean": "rm -rf dist dist.zip node_modules",
    "build": "rm -rf dist dist.zip && cpy --cwd=src --parents '**/*' '!**/*.ts' ../dist/ && cpy ./package.json dist/",
    "package": "npm prune --production && npm dedupe --production && zip -r dist.zip node_modules && cd dist && zip -ur ../dist.zip . && cd -",
    "aws:upload": "npm run package && aws s3 cp ./dist.zip s3://ftrprf-bucket-builds/$npm_package_name/branches/$BRANCH_NAME.zip",
    "test": "NODE_OPTIONS=--experimental-vm-modules npx jest test/test.js"
  },
  "dependencies": {
    "@converse/openpromise": "^0.0.1",
    "@ftrprf/judge-core": "^1.8.0",
    "@ftrprf/judge-runner": "^1.8.0",
    "busboy": "^0.3.1",
    "chrome-aws-lambda": "^10.1.0",
    "dotenv": "^10.0.0",
    "lodash-es": "^4.17.21",
    "mergebounce": "^0.1.1",
    "node-fetch": "^3.2.10",
    "puppeteer-core": "^10.1.0"
  },
  "devDependencies": {
    "cpy-cli": "^3.1.1",
    "jest": "^29.1.2",
    "node-static": "^0.7.11",
    "prettier": "^2.8.8",
    "puppeteer": "^10.1.0",
    "msw": "^1.2.1"
  },
  "prettier": {
    "singleQuote": true,
    "printWidth": 110
  }
}

index.js:

import fetch from 'node-fetch';

import chromium from 'chrome-aws-lambda';
import { puppeteerOptions } from './utils/puppeteer-options.js';
[...]

/** @type Promise<Browser> | null */
let globalBrowser = null;

/**
 * @returns {Promise<Page>}
 */
async function newPage(headless = true) {
  if (globalBrowser === null) {
    const path = await chromium.executablePath;
    globalBrowser = chromium.puppeteer.launch({
      ...puppeteerOptions,
      headless,
      path,
    });
  }

  const browser = await globalBrowser;
  return await browser.newPage();
}

[...]

export async function handler(event, _context) {
  ...
  try {
    page = await newPage(event.test);
    await page.setBypassCSP(true);

    // Link page errors
    const errorPromise = new Promise((resolve, reject) => {
      page.on('error', reject);
      page.on('pageerror', reject);
    });

    timeoutPromise = sleep(55000).then(() => {
      if (running) {
        console.error('Aborted the test runner after 60s');

        throw new Error('timeout');
      }
    });

    [...]

    console.log('Judge is shut down...');
  }

  return response;
}