writingdeveloper
writingdeveloper

Reputation: 1076

How to use Playwright package in Express.js with Node.js

I use Express.js with Node.js and I want to parse information when I access to specific route like /search.

So I create /search route with GET method and when I access to /search route.

So my search.js file is like this.

const firefox = require('playwright');
/**
 * GET /
 * Result page.
 */
exports.index = async (req, res) => {
  // let searchKeyword = req.body.search;
  (async () => {
    const browser = await firefox.launch({ headless: true });
    const context = await browser.newContext();
    const page = await context.newPage();
    await page.goto('https://example.com');
    await page.screenshot({ path: 'sample.png' });
    await browser.close();
  })();
}

But when I load this file with node app.js It shows me an error.

(node:21100) UnhandledPromiseRejectionWarning: TypeError: firefox.launch is not a function
    at C:\Users\person\Documents\GitHub\PandoraCube\controllers\search.js:24:35
    at exports.index (C:\Users\person\Documents\GitHub\PandoraCube\controllers\search.js:65:5)

When I try to use Playwright seperately it works fine, but when I put code into Express code. it doesn't works. How can I fix this problem?

I already search error message in Google but it seems that there are no similar case.

Upvotes: 0

Views: 1737

Answers (1)

hardkoded
hardkoded

Reputation: 21637

You need to fix the import:

const {firefox} = require('playwright');

Upvotes: 2

Related Questions