Go to the /functions
folder created after Firebase installation.
Install Puppeteer:
terminalnpm i puppeteer
index.js
in the /functions
folder and add the library:javascriptconst functions = require('firebase-functions'); const puppeteer = require('puppeteer');
javascriptconst 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!'); });
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.
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)
terminalfirebase deploy
Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.