Luc*_*rim 3 javascript node.js next.js puppeteer vercel
当我尝试访问上传到 vercel 服务器的 API 时,出现此错误。
有没有人有同样的错误?
当我在本地运行它时,它工作正常。
2021-02-15T19:38:59.218Z 0109b575-a2e7-478e-aefe-aa3335b5b6b8 错误错误:无法启动浏览器进程!的/ tmp /铬:错误而载入共享库:libnss3.so:无法打开共享对象文件:没有这样的文件或目录故障排除:https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md 在界面上的 onClose (/var/task/node_modules/puppeteer-core/lib/cjs/puppeteer/node/BrowserRunner.js:193:20)。(/var/task/node_modules/puppeteer-core/lib/cjs/puppeteer/node/BrowserRunner.js:183:68) 在 Interface.emit (events.js:327:22) 在 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/进程/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;
}
Run Code Online (Sandbox Code Playgroud)
dhi*_*ilt 33
感谢 @andrew-mititi 和他出色的回答,我能够在我的环境中解决这个问题,即Docker FROM node:16(即 Debian GNU/Linux 10)+ puppeteer v13(不是 puppeteer-core)。我执行了以下步骤:
make build & docker rundocker exec -it CONTAINER_ID /bin/bashls project_folder/node_modules/puppeteer/.local-chromium,就linux-961656我而言ls project_folder/node_modules/puppeteer/.local-chromium/linux-961656,它是chrome-linuxldd node_modules/puppeteer/.local-chromium/linux-961656/chrome-linux/chrome | grep not,它应该显示类似的内容 libnss3.so => not found
libnssutil3.so => not found
libsmime3.so => not found
libnspr4.so => not found
libatk-1.0.so.0 => not found
libatk-bridge-2.0.so.0 => not found
libcups.so.2 => not found
libdrm.so.2 => not found
libdbus-1.so.3 => not found
libxkbcommon.so.0 => not found
libXcomposite.so.1 => not found
libXdamage.so.1 => not found
libXfixes.so.3 => not found
libXrandr.so.2 => not found
libgbm.so.1 => not found
libasound.so.2 => not found
libatspi.so.0 => not found
Run Code Online (Sandbox Code Playgroud)
最后一步是在安装项目的 npm 依赖项后将此列表转换为正确的 apt-get 命令并将其添加到 Docker make 脚本中。就我而言,是:
RUN apt-get update && apt-get install -y libnss3 libnspr4 libatk1.0-0 libatk-bridge2.0-0 libcups2 libdrm2 libxkbcommon0 libxcomposite1 libxdamage1 libxfixes3 libxrandr2 libgbm1 libasound2
Run Code Online (Sandbox Code Playgroud)
Son*_*yen 15
对于实际使用 Vercel 而不是 AWS 的人:
问题确实出在您应用程序的 Node.js 版本上。您必须使用 Node.js 14 而不是像提到的所有其他答案一样使用 16。要在 Vercel 上执行此操作,请执行以下两件事:
14.xpackage.json,engines设置"node": "^14"// package.json
{
...
"engines": {
"node": "^14"
}
...
}
Run Code Online (Sandbox Code Playgroud)
参考:Vercel 文档
And*_*iti 13
我遇到了同样的问题,即 puppeteer 在我的本地环境中运行正常,但是当我部署到 AWS EC2 时,我遇到了同样的错误共享加载库 解决方案
最常见的原因是 Node.js v14.0.0 中的一个错误,它破坏了 extract-zip,Puppeteer 用来将浏览器下载提取到正确位置的模块。该错误已在 Node.js v14.1.0 中修复,因此请确保您运行的是该版本或更高版本。或者,如果您无法升级,您可以降级到 Node.js v12,但我们建议您尽可能升级。
对于那些在 Windows 环境中运行时可能遇到此问题的人,您可以尝试通过ignoreDefaultArgs: ['--disable-extensions']选项从代码启动它铬时即
const browser = await puppeteer.launch({ignoreDefaultArgs: ['--disable-extensions']})
Run Code Online (Sandbox Code Playgroud)
这将停用 puppeteer 的默认行为,从而禁用通常由铬/铬使用的任何扩展。
Linux 和 MacOS 使用
导致此错误的问题
UnhandledPromiseRejectionWarning:错误:无法启动浏览器进程!加载共享库时出错:libnss3.so:无法打开共享对象文件:没有这样的文件或目录
大多数但并非所有时间都是由最新版本所需的缺失引起的。好消息是您可以轻松检查导致崩溃的缺失 chrome 依赖项。
cd /project_folder/node_modules/puppeteer/.local-chromium// 用路径ls 中显示的目录替换blockqoutes 以检查目录名称
在最后一个目录 运行以下命令以检查缺少的依赖项
ldd 铬 | 不
如果您看到任何缺少的依赖项,请运行此命令以安装所有内容并重新启动您的应用程序。
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
Run Code Online (Sandbox Code Playgroud)
瞧!!一切都应该被修复
cie*_*awy 10
对于任何libnss3.so: cannot open shared object file在 aws lambda 上有问题的人。对我来说,修复方法是将chrome-aws-lambda和都升级puppeteer-core到版本 >= 6.0.0 - 这是运行时所需的最低版本runtime: nodejs14.x。
| 归档时间: |
|
| 查看次数: |
4242 次 |
| 最近记录: |