Develop a web scraper in JavaScript (or TypeScript) using Puppeteer to extract product details (name, price, and image URL) from an e-commerce website's search results page. Save the extracted data in a JSON file.
PLAINFORMAT
const fs = require('fs');
const puppeteer = require('puppeteer');

(async () => {
  // Launch browser instance and navigate to the search results page
  const browser = await puppeteer.launch({ headless: false });
  const page = await browser.newPage();
  await page.goto('https://example.com/search?q=product');
  await page.waitForSelector('.product-listing');

  // Extract product details (name, price, and image URL)
  const products = await page.$$eval('.product-item', items => {
    return items.map(item => {
      const name = item.querySelector('.product-name').innerText;
      const price = item.querySelector('.product-price').innerText;
      const imageUrl = item.querySelector('.product-image img').src;

      return { name, price, imageUrl };
    });
  });

  // Save extracted data in a JSON file
  fs.writeFileSync('products.json', JSON.stringify(products, null, 2));

  // Close browser instance and log success message
  await browser.close();
  console.log('Product details have been saved to products.json');
})();