Nodejs How to Fix `Browser. newPage Is Not a Function` Using Puppeteer-Core?

I am trying to use pupetteer-core but when I run my code.

const puppeteer = require('puppeteer-core');
module.exports= run = () => {
    const url = '
    const browser = puppeteer.launch();
    const page = browser.newPage().then(function(page){
    page.goto(url)
    return browser
};

run().catch(console.error.bind(console))

I get this error TypeError: browser.newPage is not a function

1 Answer

The problem in your code is that puppeteer works with Promises, meaning that most functions will return a Promise instead of the value directly. This means that you ether have to use then function or await statements to get the value.

Code sample

module.exports = run = async () => {
    const url = '
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto(url);
    return browser;
};

Note that the function is marked as async now, making it implicitly returning a Promise. That means to wait for the run() function to finish, you would have to call it from within another async function like this:

(async () => {
    const browser = await run();
})();
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest