MacOS:如何在有或没有 Chromium 的情况下安装 puppeteer?

Mag*_* IB 0 macos chromium npm puppeteer

我正在尝试在 macOS (Mojave) 10.14.3 上全局安装 puppeteer

  1. 第一次失败(在sudo su模式中):npm install -g puppeteer
> puppeteer@1.12.2 install /usr/local/lib/node_modules/puppeteer
> node install.js

ERROR: Failed to download Chromium r624492! Set "PUPPETEER_SKIP_CHROMIUM_DOWNLOAD" env variable to skip download.
{ Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/puppeteer/.local-chromium'
  -- ASYNC --
    at BrowserFetcher.<anonymous> (/usr/local/lib/node_modules/puppeteer/lib/helper.js:108:27)
    at Object.<anonymous> (/usr/local/lib/node_modules/puppeteer/install.js:64:16)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
  errno: -13,
  code: 'EACCES',
  syscall: 'mkdir',
  path: '/usr/local/lib/node_modules/puppeteer/.local-chromium' }
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! puppeteer@1.12.2 install: `node install.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the puppeteer@1.12.2 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /var/root/.npm/_logs/2019-02-27T14_04_04_609Z-debug.log
Run Code Online (Sandbox Code Playgroud)
  1. 第二次失败:我成功地从https://commondatastorage.googleapis.com/chromium-browser-snapshots/index.html?prefix=Mac/635943/手动安装了 Chromium ,然后(在sudo su模式下):

npm get | grep PUPPETEER

PUPPETEER_EXECUTABLE_PATH = "/Applications/Chromium.app/Contents/MacOS/Chromium"
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD = true
PUPPETEER_EXECUTABLE_PATH = "/Applications/Chromium.app/Contents/MacOS/Chromium"
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD = true
Run Code Online (Sandbox Code Playgroud)

npm install -g puppeteer

> puppeteer@1.12.2 install /usr/local/lib/node_modules/puppeteer
> node install.js

ERROR: Failed to download Chromium r624492! Set "PUPPETEER_SKIP_CHROMIUM_DOWNLOAD" env variable to skip download.
{ Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/puppeteer/.local-chromium'
  -- ASYNC --
    at BrowserFetcher.<anonymous> (/usr/local/lib/node_modules/puppeteer/lib/helper.js:108:27)
    at Object.<anonymous> (/usr/local/lib/node_modules/puppeteer/install.js:64:16)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
  errno: -13,
  code: 'EACCES',
  syscall: 'mkdir',
  path: '/usr/local/lib/node_modules/puppeteer/.local-chromium' }
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! puppeteer@1.12.2 install: `node install.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the puppeteer@1.12.2 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /var/root/.npm/_logs/2019-02-27T14_07_19_001Z-debug.log
Run Code Online (Sandbox Code Playgroud)

我在这里做错了什么?

Chu*_*ran 5

试试看:

npm config set user 0
npm config set unsafe-perm true
npm install -g puppeteer
Run Code Online (Sandbox Code Playgroud)

选项不安全烫发

设置为 true 以在运行包脚本时禁止 UID/GID 切换。如果显式设置为 false,则以非 root 用户身份安装将失败。

如果 npm 是用 root 权限调用的,那么它会将 uid 更改为用户帐户或用户配置指定的 uid,默认为 nobody。设置 unsafe-perm 标志以使用 root 权限运行脚本。