title: Puppeteer Protocol error (Runtime.callFunctionOn): Target closed
body: <p>I am new to javascript and nodejs</p>
  
  <p>I am trying to generate a PDF table using node.js using google puppeteer.</p>
  
  <p>I followed a youtube tutorial and the code below is the same as used in the tutorial. 
  Note: I dont take credit for the below code.</p>
  
  <p>I used a sample HTML template which I populate data from a json file.</p>
  
  <pre><code>    const puppeteer = require('puppeteer');
      const fs = require('fs-extra');
      const hbs = require('handlebars');
      const path = require('path');
      const data = require('./stressData2.json');
  
      const compile = async function(templateName, data){
          const filePath = await path.join(process.cwd(), 'templates', `${templateName}.hbs`);
          const html = await fs.readFile(filePath, 'utf-8');
          return await hbs.compile(html)(data);
      };
  
      (async function(){
          try{
              const browser = await puppeteer.launch();
              const page = await browser.newPage();
              const content = await compile('stressTemplate', data);
              await page.setContent(content);
              await page.pdf({
                  path: 'pdfs/mypdf.pdf',
                  format: 'A4',
                  printBackground: true
              });
  
              console.log('done');
  
              await browser.close();
              await process.exit();
          } catch(e){
              console.log('Error', e);
          }
      })();
  </code></pre>
  
  <p>I am able to stress test till 100000 results, but when I push to 200000 
  I get the below error</p>
  
  <pre><code>{ Error: Protocol error (Runtime.callFunctionOn): Target closed.
      at Promise (/userdir/node_modules/puppeteer/lib/Connection.js:186:56)
      at new Promise (&lt;anonymous&gt;)
      at CDPSession.send (/userdir/node_modules/puppeteer/lib/Connection.js:185:12)
      at ExecutionContext.evaluateHandle (/userdir/node_modules/puppeteer/lib/ExecutionContext.js:
  115:75)
      at ExecutionContext.&lt;anonymous&gt; (/userdir/node_modules/puppeteer/lib/helper.js:145:23)
      at ExecutionContext.evaluate (/userdir/node_modules/puppeteer/lib/ExecutionContext.js:58:31)
      at ExecutionContext.&lt;anonymous&gt; (/userdir/node_modules/puppeteer/lib/helper.js:145:23)
      at Frame.evaluate (/userdir/node_modules/puppeteer/lib/FrameManager.js:447:20)
    -- ASYNC --
      at Frame.&lt;anonymous&gt; (/userdir/node_modules/puppeteer/lib/helper.js:144:27)
      at Frame.setContent (/userdir/node_modules/puppeteer/lib/FrameManager.js:541:16)
      at Frame.&lt;anonymous&gt; (/userdir/node_modules/puppeteer/lib/helper.js:145:23)
      at Page.setContent (/userdir/node_modules/puppeteer/lib/Page.js:615:42)
      at Page.&lt;anonymous&gt; (/userdir/node_modules/puppeteer/lib/helper.js:145:23)
      at /userdir/git/NodeJsServer/serverPdfGenerator.js:18:20
    message: 'Protocol error (Runtime.callFunctionOn): Target closed.' }
  </code></pre>
  
  <p>I searched online and I am not able to fix this, has anyone faced this issue before
  I seen this post as well <a href="https://stackoverflow.com/questions/50645321/unhandledpromiserejectionwarning-error-protocol-error-runtime-callfunctionon">UnhandledPromiseRejectionWarning: Error: Protocol error (Runtime.callFunctionOn): Target closed. (Puppeteer)</a> but not able to resolve it</p>
  
  <p>PR Link on Github: <a href="https://github.com/GoogleChrome/puppeteer/issues/3683" rel="nofollow noreferrer">https://github.com/GoogleChrome/puppeteer/issues/3683</a></p>