zhoucheng`s Blog 10 * 10
Last updated: 2018-08-10
zhouchengdeMacBook-Pro:~ Desktop$ node puppeteer安装及chromium下载失败问题解决.js

$ Post.tags
puppeteer

$ Post.categories
工具

puppeteer安装及chromium下载失败问题解决

在根目录package.json中添加依赖

1
2
3
4
5
{
"dependencies": {
"puppeteer": "^1.6.2"
}
}

在根目录.npmrc中添加环境变量

puppeteer需要下载chromium,但下载chromium需要翻墙,如果你有翻墙的条件,请忽略此步骤

1
puppeteer_skip_chromium_download = true

安装puppeteer

1
$ npm install

手动下载chromium

如果你有翻墙的条件,请忽略此步骤

chromium官方下载网站:https://download-chromium.appspot.com/

chromium百度云下载
Linux:https://pan.baidu.com/s/1e-FXoe61FML_ummdxKvSxA
Mac:https://pan.baidu.com/s/1td5_UhJbNPni-NemeFbM-Q
Win-32:https://pan.baidu.com/s/1nIDEOvSLioJ9Ingnebxj_w
Win-64:https://pan.baidu.com/s/1qd5k8zsGBeGkyCiTQlogkg

将下载好的chromium解压,统一修改名称为chromium,复制到与项目目录同级目录

手动指定chromium程序路径

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const os = require('os');
const path = require('path');
const puppeteer = require('puppeteer');

const platform = os.platform().toLocaleLowerCase();

const options = {};

if ( platform === 'linux' ) {
/* linux环境需要去沙箱 */
options.args = ['--no-sandbox', '--disable-setuid-sandbox'];
options.executablePath = path.join(process.cwd(), '../chromium/chrome')
} else if ( platform === 'darwin' ) {
options.executablePath = path.join(process.cwd(), '../chromium/Chromium.app/Contents/MacOS/Chromium')
} else if ( platform === 'win32' ) {
options.executablePath = path.join(process.cwd(), '../chromium/chrome.exe')
}

const browser = await puppeteer.launch(options);

如果是Linux系统还要执行以下步骤

  1. 安装依赖库
    sudo yum install pango.x86_64 libXcomposite.x86_64 libXcursor.x86_64 libXdamage.x86_64 libXext.x86_64 libXi.x86_64 libXtst.x86_64 cups-libs.x86_64 libXScrnSaver.x86_64 libXrandr.x86_64 GConf2.x86_64 alsa-lib.x86_64 atk.x86_64 gtk3.x86_64 -y

  2. 安装字体

    1
    2
    3
    4
    5
    6
    7
    ## 先复制到你需要的字体文件到/usr/share/fonts
    cd /usr/share/fonts
    mkfontscale
    mkfontdir

    ## 查看字体列表
    fc-list

!完成