javascript -/tmp/chromium : error while loading shared libraries: libnss3. 所以:无法打开共享对象文件:没有这样的文件或目录 Vercel

标签 javascript node.js next.js puppeteer vercel

当我尝试访问上传到 vercel 服务器上的 API 时,我收到了这个错误。
有没有人有同样的错误?
当我在本地运行它时,它工作正常。

2021-02-15T19:38:59.218Z 0109b575-a2e7-478e-aefe-aa3335b5b6b8 ERROR Error: Failed to launch the browser process! /tmp/chromium: error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md at onClose (/var/task/node_modules/puppeteer-core/lib/cjs/puppeteer/node/BrowserRunner.js:193:20) at Interface. (/var/task/node_modules/puppeteer-core/lib/cjs/puppeteer/node/BrowserRunner.js:183:68) at Interface.emit (events.js:327:22) at Interface.close (readline.js:424:8) at Socket.onend (readline.js:202:10) at Socket.emit (events.js:327:22) at endReadableNT (internal/streams/readable.js:1327:12) at processTicksAndRejections (internal/process/task_queues.js:80:21)


代码
import puppeteer, { Page } from 'puppeteer-core'
import chrome from 'chrome-aws-lambda'

export async function getOptions() {
  const isDev = !process.env.AWS_REGION
  let options;

  const chromeExecPaths = {
    win32: 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe',
    linux: '/usr/bin/google-chrome',
    darwin: '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
  }
  
  const exePath = chromeExecPaths[process.platform]

  if (isDev) {
    options = {
      args: [],
      executablePath: exePath,
      headless: true
    }
  } else {
    options = {
      args: chrome.args,
      executablePath: await chrome.executablePath,
      headless: chrome.headless
    }
  }

  return options
}

let _page: Page | null
async function getPage(): Promise<Page> {
  if (_page) {
    return _page
  }

  const options = await getOptions()
  const browser = await puppeteer.launch(options)

  _page = await browser.newPage()

  return _page
}


export async function getScreenshot(html: string, { width, height } = { width: 800, height: 800 }) {
  const page = await getPage();

  await page.setContent(html);
  await page.setViewport({ width, height });

  const file = await page.screenshot({ type: 'png' });

  return file;
}

最佳答案

我遇到了同样的问题,即 puppeteer 在我的本地环境中运行良好,但是当我部署到 AWS EC2 时,我遇到了同样的错误 共享加载库
解决方案

  • 首先检查你正在运行的nodejs的版本,如果不是低于 v14.0 或高于 v14.0 .升级或降级(建议升级)并重新启动您的应用程序然后尝试。

  • The most common cause is a bug in Node.js v14.0.0 which broke extract-zip, the module Puppeteer uses to extract browser downloads into the right place. The bug was fixed in Node.js v14.1.0, so please make sure you're running that version or higher. Alternatively, if you cannot upgrade, you could downgrade to Node.js v12, but we recommend upgrading when possible.


  • 琐碎:-确保您的项目文件夹所在的目录不属于root。

  • 在windows环境下运行时可能遇到此问题的 friend 可以尝试通过 ignoreDefaultArgs: ['--disable-extensions'] 从代码启动 Chrome 时的选项,即
    const browser = await puppeteer.launch({ignoreDefaultArgs: ['--disable-extensions']})
    
    这将停用 puppeteer 的默认行为,以禁用 chromium/chrome 通常使用的任何扩展。
    Linux 和 MACOS 使用
    导致此错误的问题

    UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process! error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory TROUBLESHOOTING


    大部分但并非所有时间都是由于缺少最新版本中所需的依赖项造成的。好消息是您可以轻松检查导致崩溃的缺失 chrome 依赖项。
  • 确保您位于项目的根文件夹中
  • 导航到 node_modules 文件夹
  • 导航到安装 puppeteer chrome Linux 工具的文件夹

    • cd /project_folder/node_modules/puppeteer/.local-chromium/linux-some number/chrome-linux
      replace the linux-some number with whatever ls will output
    • ls at /.local-chromium to check name of your directory

  • 在最后一个目录 [ chrome-linux ] 运行以下命令以检查缺少的依赖项
    ldd Chrome |不是

  • 如果您看到任何缺少的依赖项,请运行此命令以安装所有内容并重新启动您的应用程序。
    sudo apt-get install ca-certificates fonts-liberation libappindicator3-1 libasound2 libatk-bridge2.0-0 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgbm1 libgcc1 libglib2.0-0 libgtk-3-0 libnspr4 libnss3 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 lsb-release wget xdg-utils
    
    瞧!!一切都应该修复

    关于javascript -/tmp/chromium : error while loading shared libraries: libnss3. 所以:无法打开共享对象文件:没有这样的文件或目录 Vercel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66214552/

    相关文章:

    javascript - 与另一个实例进行比较时,我的类实例如何隐式返回一个数字?

    javascript - i18n 支持与下一次导出不兼容。 (SSR - NextJS 10)

    Next.js 多语言站点的 url 结构

    javascript - 页面加载时如何调用 AnimateScroll 插件?

    javascript - 在 Google Chrome 中创建面板

    javascript - 从客户端设置和清除 Node 服务器上的超时

    node.js - Rich Embed 中的 Discord.js 标签 channel

    node.js - 更改文件时如何让工头重新加载 node.js?

    reactjs - 如何在使用 Next.js 完成的外部项目中使用 Storybook 组件(和 Lerna)?

    javascript - Knockout 可排序绑定(bind)顺序