title: Error: Evaluation Failed: ReferenceError: util is not defined
body: <p>I am trying to pass the util module object to puppeteer <code>page.evaluate</code> with no success. I understand this question was asked in <a href="https://stackoverflow.com/questions/48815565/how-to-pass-required-module-object-to-puppeteer-page-evaluate">How to pass required module object to puppeteer page.evaluate
  </a> but the solution provided does not work in my case. MWE:</p>
  
  <pre><code>const puppeteer = require("puppeteer");
  
  const path = "http://books.toscrape.com/";
  
  // scrape funs 
  (async () =&gt;{
      const browser = await puppeteer.launch({headless: false});
      const page = await browser.newPage();
      await page.goto(path, {waitUntil: "networkidle2", timeout: 0});
      await page.waitFor(1000);
      await page.addScriptTag({path: './node_modules/util/util.js'});
      // selector with replaceable element
      const buttonText = await page.evaluate(() =&gt; {
          let selectorButton = "#default &gt; div &gt; div &gt; div &gt; div &gt; section &gt; div:nth-child(2) &gt; ol &gt; li:nth-child(%s) &gt; article &gt; div.product_price &gt; form &gt; button";
          let buttons = [];
          for(let i = 1; i &lt; 21; i ++){
              let textOut = document.querySelector(util.format(selectorButton, i)).innerText;
              buttons.push(textOut);
          };
          return buttons;  
      });
  
  // return
  await browse.close();
  console.log(buttonText);
  })();
  </code></pre>
  
  <p>Shows error:</p>
  
  <blockquote>
    <p>UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Evaluation failed: ReferenceError: util is not defined</p>
  </blockquote>
  
  <p>Thank you</p>
  
  <h2> Edit 1 </h2>
  
  <p>Adding <code>const util = require("util");</code> in the initial lines and doesn't work as shown in <a href="https://stackoverflow.com/questions/48815565/how-to-pass-required-module-object-to-puppeteer-page-evaluate">How to pass required module object to puppeteer page.evaluate</a>.</p>
  
  <h2> Edit 2 </h2>
  
  <p>Even when I use browserify I can't seem to inject the <code>util</code>module into the puppeteer page. Steps:</p>
  
  <p>On project <code>PATH</code>, create <code>main.js</code> as follows:
  <code>var util = require('util');</code></p>
  
  <p>Then on <code>PATH</code> in terminal: <code>browserify main.js -o bundle.js</code>. A file <code>bundle.js</code> appears in the project <code>PATH</code>. </p>
  
  <p>Then run the following:</p>
  
  <pre><code>const puppeteer = require("puppeteer");
  const path = "http://books.toscrape.com/";
  
  // scrape funs 
  (async () =&gt;{
      const browser = await puppeteer.launch({headless: false});
      const page = await browser.newPage();
      await page.goto(path, {waitUntil: "networkidle2", timeout: 0});
      await page.waitFor(1000);
      await page.addScriptTag({path: "main.js"});
      await page.addScriptTag({path: "bundle.js"});
      // selector with replaceable element
      const buttonText = await page.evaluate(() =&gt; {
          let buttons = [];
          let selectorButton = "#default &gt; div &gt; div &gt; div &gt; div &gt; section &gt; div:nth-child(2) &gt; ol &gt; li:nth-child(%s) &gt; article &gt; div.product_price &gt; form &gt; button";
          for(let i = 1; i &lt; 21; i ++){
              let textOut = document.querySelector(util.format(selectorButton, i)).innerText;
              buttons.push(textOut);
          };
          return buttons;  
      });
  
  // return
  await browse.close();
  console.log(buttonText);
  })();
  </code></pre>
  
  <p>Error:</p>
  
  <blockquote>
    <p>UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Evaluation failed: TypeError: Cannot read property 'format' of undefined
        at :5:55</p>
  </blockquote>