错误代码:

path/node_modules/puppeteer/.local-chromium/linux-1011831/chrome-linux/chrome: error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory
可能原因:node这个容器不是一个完整的linux,缺少必要的依赖

检查下缺少的依赖,注意更换自己的版本号,自行去文件夹查看即可

ldd node_modules/puppeteer/.local-chromium/linux-1011831/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 

安装依赖

sudo 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

再次检查所有的依赖,如果没有提示就证明已安装完全。

再次运行项目即可

再补充一个错误,如果你在docker中运行大概率会出现,

Error: Failed to launch the browser process!                                                                                         
[0713/113113.449566:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.

出现此错误的原因是,在运行时是用root账户运行chrome

解决方法:
const browser = await puppeteer.launch({args: ['--no-sandbox']});

截图时 中文显示方框

原因:系统无中文字体

解决方法
cp *.ttf /usr/share/fonts

fc-cache -fv

Done~

最后修改:2022 年 07 月 13 日
如果觉得我的文章对你有用,请随意赞赏