pkg icon indicating copy to clipboard operation
pkg copied to clipboard

Puppeteer support?

Open astefanutti opened this issue 9 years ago • 98 comments

I've been able to package an application that requires Puppeteer. However the package application is unable to spawn the Chromium instance process from the bundled Chromium binary:

events.js:182
      throw er; // Unhandled 'error' event
      ^

Error: spawn /snapshot/decktape/node_modules/puppeteer/.local-chromium/mac-494755/chrome-mac/Chromium.app/Contents/MacOS/Chromium ENOENT
    at exports._errnoException (util.js:1026:11)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:189:19)
    at onErrorNT (internal/child_process.js:366:16)
    at _combinedTickCallback (internal/process/next_tick.js:102:11)
    at process._tickCallback (internal/process/next_tick.js:161:9)
    at Function.Module.runMain (pkg/prelude/bootstrap.js:1282:13)
    at startup (bootstrap_node.js:200:16)
    at bootstrap_node.js:617:3

Pointing to an external Chromium binary works.

So it seems something similar to #147 is required.

astefanutti avatar Aug 23 '17 08:08 astefanutti

@igorklopov , thanks for the quick patch.

Unfortunately, running a pkg packaged puppeteer based script, on Ubuntu 16.04 failed with the following assertion error: AssertionError [ERR_ASSERTION]: Chromium revision is not downloaded. Run npm install

Any ideas how to solve this?

hemulin avatar Aug 25 '17 08:08 hemulin

Did you follow the warning? Please post pkg output during compilation.

igorklopov avatar Aug 25 '17 09:08 igorklopov

No errors whatsoever:

┌─[11:40:30]─[hemulin@neon-machine] [scripts U5]
└─[.../neon-crawlers/src/js]─> pkg main.js 
> [email protected]
> Targets not specified. Assuming:
  node8-linux-x64, node8-macos-x64, node8-win-x64
┌─[11:40:42]─[hemulin@neon-machine] [scripts U8]
└─[.../neon-crawlers/src/js]─> ./main-linux 
{ AssertionError [ERR_ASSERTION]: Chromium revision is not downloaded. Run npm install
    at Console.assert (console.js:165:23)
    at Function.launch (/snapshot/js/node_modules/puppeteer/lib/Launcher.js:0:0)
    at Function.launch (/snapshot/js/node_modules/puppeteer/lib/Puppeteer.js:0:0)
    at __dirname (/snapshot/js/main.js:0:0)
    at Object.<anonymous> (/snapshot/js/main.js:0:0)
    at Module._compile (pkg/prelude/bootstrap.js:1226:22)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
  generatedMessage: false,
  name: 'AssertionError [ERR_ASSERTION]',
  code: 'ERR_ASSERTION',
  actual: false,
  expected: true,
  operator: '==' }

Tried to run the test you added to the patch

Running directly with node had no problem:

┌─[11:50:42]─[hemulin@neon-machine] [scripts U12]
└─[.../neon-crawlers/src/js]─> node test.js 
ok

But got the same issue when packaged:

┌─[11:50:33]─[hemulin@neon-machine] [scripts U9]
└─[.../neon-crawlers/src/js]─> pkg test.js 
> [email protected]
> Targets not specified. Assuming:
  node8-linux-x64, node8-macos-x64, node8-win-x64
┌─[11:50:39]─[hemulin@neon-machine] [scripts U12]
└─[.../neon-crawlers/src/js]─> ./test-linux 
(node:32440) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): AssertionError [ERR_ASSERTION]: Chromium revision is not downloaded. Run npm install
(node:32440) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

hemulin avatar Aug 25 '17 09:08 hemulin

You should see a warning that asks you to ship .local-chromium directory as puppeteer next to executable. But i don't see that warning in your compilation output.

igorklopov avatar Aug 25 '17 10:08 igorklopov

Not sure what you mean. I've copied the .local-chromium folder from the node_modules/puppeteer/.local-chromium up the tree and put it in the root of the project, next to the main js files. renamed it to puppeteer, packaged again and nothing changed. Still the same errors

Tree presentation of the folder:

┌─[12:22:13]─[hemulin@neon-machine] [scripts U13]
└─[.../neon-crawlers/src/js]─> tree -L 1
.
├── main.js
├── main-linux
├── main-macos
├── main-win.exe
├── node_modules
├── package.json
├── package-lock.json
├── puppeteer
├── test.js
├── test-linux
├── test-macos
└── test-win.exe

hemulin avatar Aug 25 '17 10:08 hemulin

I'm having the same issue as @hemulin . When trying to run the app:

❯ ./app-macos
(node:48332) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): AssertionError [ERR_ASSERTION]: Chromium revision is not downloaded. Run "npm install"
(node:48332) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Output when compiling:

❯ pkg app.js
> [email protected]
> Targets not specified. Assuming:
  node8-linux-x64, node8-macos-x64, node8-win-x64

/www/pkg-critical-2
❯ echo $status
0

I've copied the node_modules/puppeteer/.local-chromium as ./puppeteer in the pkg root.

❯ tree -L 1
.
├── app-linux
├── app-macos
├── app-win.exe
├── app.js
├── node_modules
├── package.json
├── puppeteer
└── yarn.lock

2 directories, 7 files

/apps/pkg-critical-2
❯ cd puppeteer/

/apps/pkg-critical-2/puppeteer
❯ tree -L 1
.
└── mac-497674

1 directory, 0 files

I wonder if there is something weird going on with the dynamic directory names for the .local-chromium contents. It seems the directory name mac-497674 changes based off of version.

But, I'm not seeing anything hardcoded for it anywhere in the code base, so, it most likely handles this automatically.

wdavis avatar Sep 13 '17 19:09 wdavis

After many attempts, I was able to find a work-around incase anyone needs a temporary solution:

var path = require('path');
if (process.pkg) {
  var puppeteer = require(path.resolve(process.cwd(), 'puppeteer'));
} else {
  var puppeteer = require('puppeteer');
}

In this solution, just put a copy of puppeteer next to your executable. It's not pretty but I hope it helps someone!

tferullo avatar Sep 20 '17 15:09 tferullo

Hi @tferullo - Can you please share a sample repo? I still cannot get it working in Windows.

image

EDIT: Works now after using pkg -t node8-win32 package.json to compile along with tferullo's workaround.

rajyraman avatar Sep 20 '17 19:09 rajyraman

@igorklopov I got the same issue, spawn chromium fail after pkg.

download chromium from

const downloadURLs = {
  linux: 'https://storage.googleapis.com/chromium-browser-snapshots/Linux_x64/%d/chrome-linux.zip',
  mac: 'https://storage.googleapis.com/chromium-browser-snapshots/Mac/%d/chrome-mac.zip',
  win32: 'https://storage.googleapis.com/chromium-browser-snapshots/Win/%d/chrome-win32.zip',
  win64: 'https://storage.googleapis.com/chromium-browser-snapshots/Win_x64/%d/chrome-win32.zip',
};

such as 499413, then

childProcess.spawn(chromiumPath, chromiumArgs)

got

spawn /snapshot/page2pdf/node_modules/puppeteer/.local-chromium/linux-499413/chrome-linux/chrome ENOENT

zenxds avatar Sep 30 '17 06:09 zenxds

I have the same problem. Has anyone got a solution? Possibly for electron-builder..

thatsnoi avatar Nov 03 '17 09:11 thatsnoi

I'm not sure where to write this but I've found an issue with integrating pkg and puppeteer. The puppeteer API has a method page.evaluate, accepting a function to be executed in the context of the page. This entails serializing the function and passing it over the socket to the Chromium process, to executed there. Implementation wise puppeteer calls toString() on said function.

But since pkg compiles scripts using v8::ScriptCompiler the sources are not kept. This means f.toString() will return something like 'function f() { [native code] }' to be passed over the socket. This will obviously fail with a rather cryptic error message: Error: Evaluation failed: SyntaxError: Unexpected identifier One workaround is to only pass strings to be evaluated as expressions to page.evaluate instead.

doomdavve avatar Nov 20 '17 08:11 doomdavve

There is an EcmaScript stage-3 proposal that makes it mandatory to return source code in toString() for any function implemented in Javascript: http://2ality.com/2016/08/function-prototype-tostring.html

mo avatar Nov 20 '17 18:11 mo

I am getting the following error, any idea what does it mean?

> [email protected]
> Targets not specified. Assuming: node8-linux-x64, node8-macos-x64, node8-win-x64
> Warning Cannot include directory %1 into executable. 
The directory must be distributed with executable as %2. 
  node_modules\puppeteer\.local-chromium
  path-to-executable/puppeteer

herve76 avatar Nov 24 '17 14:11 herve76

@jsappme It means that you have to copy the folder node_modules\puppeteer\.local-chromium to the folder where your executable (the file generated by pkg) is and rename that folder into puppeteer

But I still didn't succeed to use puppeteer with pkg. I get this error when I'm executing my binary:

{ Error: Cannot find module './lib/Puppeteer'
1) If you want to compile the package/file into executable, please pay attention to compilation warnings and specify a literal in 'require' call. 2) If you don't want to compile the package/file into executable and want to 'require' it from filesystem (likely plugin), specify an absolute path in 'require' call using process.cwd() or process.execPath.

I'm still investigating

Final update: Ok I was wrong, you can just include the code using scripts option like:

"pkg": {
    "scripts": "node_modules/puppeteer/lib/*.js"
  }

clementai avatar Nov 24 '17 15:11 clementai

But as @doomdavve say a lot of puppeteer function will not work with pkg as the code get compiled and so not usable with evaluate.

So I think we can't use puppeteer with pkg for now

clementai avatar Nov 24 '17 16:11 clementai

I tried all the solutions above but I am still getting:


> Warning Cannot include directory %1 into executable.
  The directory must be distributed with executable as %2.
  node_modules\puppeteer\.local-chromium
  path-to-executable/puppeteer

here is my packages:

{
  "name": "htest",
  "license": "ISC",
  "bin": "index.js",
  "start": "micro index.js",
  "dependencies": {
    "express": "4.15.2",
    "puppeteer": "^0.13.0"
  },
  "pkg": {
    "scripts": "node_modules/puppeteer/lib/*.js",
    "assets": [
      "views/**/*"
    ],
    "targets": [
      "node8-win32"
    ]
  }
}

herve76 avatar Nov 24 '17 17:11 herve76

@doomdavve can you give an example of what you mean by the potential work around that you list?

daihashi avatar Nov 27 '17 22:11 daihashi

This works for me

{
  "pkg": {
    "scripts":  "node_modules/puppeteer/lib/*.js"
  }
}
cp -R ./node_modules/puppeteer/.local-chromium ./puppeteer

reggi avatar Dec 01 '17 12:12 reggi

Solution from @reggi will partially work, but function like the one to inject CSS in the page will not, because the code that needs to be injected will be compiled and so not injectable anymore

Like here https://github.com/GoogleChrome/puppeteer/blob/master/lib/FrameManager.js#L463

clementai avatar Dec 01 '17 12:12 clementai

@Uhsac, @reggi I setup like you did with the copy of the chromium to local source tree But it still throws the exception.

Could you please explain a bit more in detail how to get this working?

"pkg": {
    "scripts": "node_modules/puppeteer/lib/*.js",
    "assets": [
      "views/**/*"
    ],
    "targets": [
      "node8-win32"
    ]
  }

my build.sh

cp -R ./node_modules/puppeteer/.local-chromium ./puppeteer
pkg index.js --out-path ../build

voordev avatar Jan 02 '18 09:01 voordev