TooTallNate / proxy-agents

Node.js HTTP Proxy Agents Monorepo
https://proxy-agents.n8.io
872 stars 229 forks source link

Running `esbuild`-bundled script crashes with `ENOENT` (related to `vm2`) #197

Closed karlhorky closed 1 year ago

karlhorky commented 1 year ago

Partly related to the original issue in the old repo:

Copying my issue report from https://github.com/puppeteer/puppeteer/issues/10352, since puppeteer-core now depends on proxy-agent and other packages in this repo:


Bug expectation

I expected to be able to bundle my Puppeteer script with esbuild

My esbuild-bundled Puppeteer script failed (see full error output below) because of the transitive dependency on vm2 (via puppeteer-core -> proxy-agent -> pac-proxy-agent -> pac-resolver -> degenerator -> vm2)

$ node abc.mjs
node:fs:601
  handleErrorFromBinding(ctx);
  ^

Error: ENOENT: no such file or directory, open '/home/runner/work/project/project/bridge.js'
    at Object.openSync (node:fs:601:3)
    at Object.readFileSync (node:fs:469:35)
    at ../../node_modules/@puppeteer/browsers/node_modules/vm2/lib/vm.js (file:///home/runner/work/project/project/abc.mjs:52297:66)
    at __require2 (file:///home/runner/work/project/project/abc.mjs:20:50)
    at ../../node_modules/@puppeteer/browsers/node_modules/vm2/lib/main.js (file:///home/runner/work/project/project/abc.mjs:54169:9)
    at __require2 (file:///home/runner/work/project/project/abc.mjs:20:50)
    at ../../node_modules/@puppeteer/browsers/node_modules/vm2/index.js (file:///home/runner/work/project/project/abc.mjs:54197:22)
    at __require2 (file:///home/runner/work/project/project/abc.mjs:20:50)
    at ../../node_modules/@puppeteer/browsers/node_modules/degenerator/dist/index.js (file:///home/runner/work/project/project/abc.mjs:54211:17)
    at __require2 (file:///home/runner/work/project/project/abc.mjs:20:50) {
  errno: -2,
  syscall: 'open',
  code: 'ENOENT',
  path: '/home/runner/work/project/project/bridge.js'
}

Node.js v18.16.0

It appears to be related to this current problem with bundling vm2:

Seems like the first version that this happens with is puppeteer-core@20.4.0, which upgraded @puppeteer/browsers from 1.3.0 to 1.4.0, which added proxy-agent.

Minimal, reproducible example

Bundle any Puppeteer script such as the simple script below via esbuild like this:

$ esbuild index.ts --bundle --outfile=abc.mjs --platform=node --target=node18 --format=esm

index.ts

import puppeteer from 'puppeteer'; // TS/ESM are all supported.

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://news.google.com/news/');
  await page.screenshot({path: 'news.png', fullPage: true});
  await browser.close();
})();

Error string

ENOENT: no such file or directory, open '/home/runner/work/project/project/bridge.js'

Puppeteer version

20.5.0

Node version

18.16.0

Package manager

pnpm

Package manager version

8.6.1

Operating system

macOS

cnlewis3 commented 1 year ago

Just adding that I'm also seeing this when trying to deploy puppeteer v20.4.0 to AWS Lambda, same line of reasoning as mentioned above.

TooTallNate commented 1 year ago

The linked vm2 issue at least provides a workaround:

I have to copy these files to dist

But ya, since this is out of the control of these packages, I will close.

OrKoN commented 11 months ago

I saw the following warning: npm WARN deprecated vm2@3.9.19: The library contains critical security issues and should not be used for production! The maintenance of the project has been discontinued. Consider migrating your code to isolated-vm. Will it be difficult to migrate? Perhaps migration will solve this issue too.