Shah Rukh K
Shah Rukh K

Reputation: 599

Implementing Puppeteer in node.js

I have my template which I want to export in pdf. The engine I am using is of PUG. I have this file in which I make a GET call to export the pdf. But when I start my server - node index.js I get error (shared below)

const express = require ('express');
var router = express.Router();
const puppeteer = require('puppeteer');


router.get('/export/html', (res,req) => {

    res.render('template');
});

router. get('/export/pdf', (req, res) => {
    (async () => {
        try {
            const browser = await puppeteer.launch();
            const page = await browser.newPage();
            await page.goto('https://localhost:3000/export/html');
        
            // Get the "viewport" of the page, as reported by the page.
            const dimensions = await page.evaluate(() => {
            return {
                width: document.documentElement.clientWidth,
                height: document.documentElement.clientHeight,
                deviceScaleFactor: window.devicePixelRatio
            };
            });
            console.log('Dimensions:', dimensions);
        
            await browser.close();
        } catch(e) {
            console.log(e);
        }
    })();
});

module.exports = router;
$ node index.js
E:\16pf\node_modules\puppeteer\lib\cjs\puppeteer\common\Page.js:707
        catch {
              ^

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.<anonymous> (E:\16pf\node_modules\puppeteer\lib\cjs\puppeteer\common\Target.js:19:19)
    at Module._compile (module.js:653:30)
    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)

Shahrukh@DESKTOP-CQ0JNTJ MINGW64 /e/16pf
$

I am not sure why is this error occurring. Any help is appreciated.

Upvotes: 0

Views: 1400

Answers (2)

Jeff Luyet
Jeff Luyet

Reputation: 502

Nothing more than a javascript syntax error. Edit the line 707 with Nano:

nano +707 "E:\16pf\node_modules\puppeteer\lib\cjs\puppeteer\common\Page.js"

Change:

catch {

To:

catch(err) {

vsemozhebuty's answer may be better because even after you fix said javascript errors, you may have to fix a bunch more in order to get it to work.

Upvotes: 0

vsemozhebuty
vsemozhebuty

Reputation: 13772

You may use an old Node.js version than does not support optional catch binding. See support list: https://node.green/#ES2019-misc-optional-catch-binding

Upvotes: 1

Related Questions