【问题标题】:Implementing Puppeteer in node.js在 node.js 中实现 Puppeteer
【发布时间】:2021-03-02 02:34:35
【问题描述】:

我有我想以 pdf 格式导出的模板。我使用的引擎是PUG的。我有这个文件,我在其中进行 GET 调用以导出 pdf。 但是当我启动我的服务器时 - 节点 index.js 我收到错误(在下面分享)

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
$

我不确定为什么会发生此错误。任何帮助表示赞赏。

【问题讨论】:

    标签: javascript node.js google-chrome puppeteer node-modules


    【解决方案1】:

    只不过是一个 javascript 语法错误。用 Nano 编辑第 707 行:

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

    变化:

    catch {
    

    收件人:

    catch(err) {
    

    vsemozhebuty 的答案可能会更好,因为即使在您修复了上述 javascript 错误之后,您可能还需要修复更多才能使其正常工作。

    【讨论】:

      【解决方案2】:

      您可以使用不支持 optional catch binding 的旧 Node.js 版本。查看支持列表:https://node.green/#ES2019-misc-optional-catch-binding

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-01-10
        • 2017-06-30
        • 2022-07-07
        • 1970-01-01
        • 2019-08-18
        • 1970-01-01
        • 2021-01-18
        相关资源
        最近更新 更多