Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
886 views
in Technique[技术] by (71.8m points)

node.js - How to resolve “Target closed” error using nodeJS and Puppeteer in docker container?

i am trying to generate pdf file using puppeteer which is being setup inside docker. however i keep getting error

Error in pdf ::---> Error: Protocol error (Page.navigate): Target closed. at /usr/src/filemanager/node_modules/puppeteer/lib/Connection.js:183:56

const getPdfFile = async (url) => {
  let browser = null
  let result

  try {
    browser = await puppeteer.launch({
      args: ["--no-sandbox", "--disable-dev-shm-usage"],
      headless: true
    })
    const page = await browser.newPage()
    await page.setDefaultNavigationTimeout(0);
    await page.goto(url, { waitUntil: 'networkidle2' })
    await page.emulateMediaType('screen')
    result = await page.pdf(defaultPrintOptions)
  } catch (error) {
    console.log('Error in pdf ::--->', error)
    throw error
  } finally {
    if (browser !== null) {
      await browser.close()
    }
  }

  return result
}

this works on my local machine environment with same docker setup however failing when deployed to cloud. i am failing to understand what mistake is being made here by me.

package.json --> "puppeteer": "^2.1.1" docker container -->

nikolaik/python-nodejs:python3.7-nodejs12-stretch
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - 
    && sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' 
    && apt-get update 
    && apt-get install -y google-chrome-unstable fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst fonts-freefont-ttf libxss1 libxtst6  
    --no-install-recommends 
    && rm -rf /var/lib/apt/lists/*
question from:https://stackoverflow.com/questions/65886538/how-to-resolve-target-closed-error-using-nodejs-and-puppeteer-in-docker-contai

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...