Puppeteer and Firebase: configuration and simple example

  1. Go to the /functions folder created after Firebase installation.

  2. Install Puppeteer:

terminal
npm i puppeteer
  1. Open index.js in the /functions folder and add the library:
javascript
const functions = require('firebase-functions');
const puppeteer = require('puppeteer');
  1. Create a function:
javascript
const functions = require('firebase-functions');
const puppeteer = require('puppeteer');

/**
 * Function to explore the links of a webpage
 * It returns 'Done'! as response
 */
exports.exploreLinks = functions.https.onRequest(async (request, response) => {
    
    // Remember that in Firebase you need to launch Puppeteer in headless mode and without sandbox
    const browser = await puppeteer.launch({ headless: true, args: ['--no-sandbox', '--disable-setuid-sandbox'] });
    
    // Opening new page
    const page = await browser.newPage();
    
    // Going to a website and waiting until it's completely loaded (works for React pages too)
    await page.goto('https://erikmartinjordan.com', { waitUntil: 'networkidle0' });
    
    // Getting all the links on the webpage
    let pageLinks = await page.$$eval('a', links => links.map(link => link.href));
    
    // Closing the browser
    await browser.close();
    
    // Sending responsse
    response.send('Done!');    
    
});
  1. Important: Upgrade the function memory (256 MB by default) to avoid memory limit exceeded error:

Firebase Console > Functions > Dashboard

At this point, hover the function and Click on . Now click on Detailed usage stats. From the Google Cloud Console, edit the function and allocate at least 1 GiB of memory.

  1. Upgrade to Node version 10 from package.json:
json
  "engines": {
    "node": "10"
  }

This will avoid the following error:

SyntaxError: Unexpected token { at createScript (vm.js:80:10) at Object.runInThisContext (vm.js:139:10) at Module._compile (module.js:617:28) at Object.Module._extensions..js (module.js:664:10) at Module.load (module.js:566:32) at tryModuleLoad (module.js:506:12) at Function.Module._load (module.js:498:3) at Module.require (module.js:597:17) at require (internal/module.js:11:18) at Object. (/srv/node_modules/puppeteer/lib/cjs/puppeteer/common/Target.js:19:19)

  1. Deploy the function:
terminal
firebase deploy

Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.