serverless & aws lambda & puppeteer & Node.js

587 Views Asked by At

I have a AWS lambda function that uses this function:

const chromium = require("chrome-aws-lambda");

export async function getGifFromGfy(url) {
  let result = null;
  let browser = null;

  try {
    browser = await chromium.puppeteer.launch({
      args: chromium.args,
      defaultViewport: chromium.defaultViewport,
      executablePath: await chromium.executablePath,
      headless: chromium.headless,
      ignoreHTTPSErrors: true,
    });

    const page = await browser.newPage();

    await page.goto("https://gfycat.com/tightwarlikefrillneckedlizard");

    result = await page.evaluate(() => {
      let elements = document.getElementsByTagName("source");
      for (let element of elements)
        if (element.type === "video/mp4" && !element.src.includes("mobile")) {
          return element.src;
        }
      return null;
    });
  } catch (error) {
    console.error(error);
    return null;
  } finally {
    if (browser !== null) {
      await browser.close();
    }
  }
  return result;
}

When I'm trying to use sls deploy -v I'm getting an error that says:

C:\Users\Omri\Desktop\projects\redditgram\src\lib\scrapeGif.js
  21:22  error  'document' is not defined  no-undef

The whole day I'm trying to figure what's the problem but nothing :( I've also tried to use this: https://www.serverless.com/examples/aws-node-puppeteer but still, I'm getting the same error.

this is my dependencies:

  "devDependencies": {
    "babel-core": "^6.25.0",
    "babel-loader": "^7.1.1",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-polyfill": "^6.23.0",
    "babel-preset-env": "^1.6.0",
    "puppeteer": "^5.3.1",
    "serverless-bundle": "^1.3.3",
    "serverless-pseudo-parameters": "^2.5.0",
    "webpack": "^4.35.2"
  },
  "dependencies": {
    "@ant-design/icons": "^4.2.2",
    "@middy/core": "^1.4.0",
    "@middy/http-cors": "^1.4.0",
    "@middy/http-error-handler": "^1.4.0",
    "@middy/http-json-body-parser": "^1.4.0",
    "aws-sdk": "^2.639.0",
    "chrome-aws-lambda": "^5.3.1",
    "http-errors": "^1.8.0",
    "node-fetch": "^1.7.3"
  }
0

There are 0 best solutions below