puppeteer: Can't found module ChromiumRevision building by webpack

  • Puppeteer version: 1.2.0
  • Platform / OS version: Windows 10
  • Node.js version: 8.9.1

I use puppeteer in a production build, and I build my server by webpack, and there is a line of code:

node_modules\puppeteer\lib\Launcher.js

const ChromiumRevision = require(path.join(helper.projectRoot(),'package.json')).puppeteer.chromium_revision;

Inside projectRoot there is:

node_modules\puppeteer\lib\helper.js

static projectRoot() {
  if (!projectRoot) {
    // Project root will be different for node6-transpiled code.
    projectRoot = fs.existsSync(path.join(__dirname, '..', 'package.json')) ? path.join(__dirname, '..') : 
    path.join(__dirname, '..', '..');
  }
  return projectRoot;
}

Webpack has to resolve this path during building and put package.json it in a separate module. But looks like it can’t do it, seems something wrong with __dirname, option in webpack config node: __dirname: false/true doesn’t help.

Is it ok to use puppeteer in build, like in one file: server.js? Is it ok to bundle puppeteer by webpack? Any workarounds?

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 5
  • Comments: 17 (4 by maintainers)

Commits related to this issue

Most upvoted comments

webpack.config.js

export default {
  ...
  externals: ['aws-sdk', 'puppeteer'],
}

Ok guys, I’m on Mac OS and here the solution which I did and work in order to use pupperteer in one single file:

STEP 1: in webpack.config.js add this:

//...
  externals: {
   //...
    puppeteer: 'require("puppeteer")'
  }

STEP 2: Install those dependencies and update again your webpack.config.js according this stackoverflow comment

STEP 3: sudo npm install -g puppeteer --unsafe-perm=true

It should works after 😃

I really don’t think that this is an issue that would be appreciated in the webpack issue tracker, though, since some of puppeteer’s code would be near-impossible to support for any bundler (e.g. dynamic requires).

But it’s also not strictly an issue with puppeteer.

I do believe that bundling is an important part of the Javascript ecosystem that any project should strive to support, however.

That being said, if you’re not afraid to override some of puppeteer’s code, here are the necessary changes to make it work with webpack:

In node_modules/puppeteer/lib/Launcher.js

Change

const ChromiumRevision = require(path.join(helper.projectRoot(), 'package.json')).puppeteer.chromium_revision;

into

const ChromiumRevision = require('../package.json').puppeteer.chromium_revision;

(as pointed out by @kutsokon already)

In node_modules/puppeteer/lib/BrowserFetcher.js (in case you need this module)

Change

const request = require(driver).request(options, res => {

into

const request = require('https').request(options, res => {

Lastly, the required bits in the webpack configuration:

const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

module.exports = {
  target: 'node',
  optimization: {
    // Disables the built-in UglifyJsPlugin
    minimize: false,
  },
  plugins: [
    new UglifyJsPlugin({
      uglifyOptions: {
        // Necessary for puppeteer, otherwise there will be odd bugs
        keep_fnames: true,
      },
    }),
  ],
};

Evidently puppeteer accesses some functions dynamically as well and therefore doesn’t play nicely with UglifyJS. Keeping function names fixes some odd runtime issues, but I haven’t done the research where in puppeteer’s code one would have to make changes to address that.

All of these changes were tested with webpack@4. Hope it helps! 😃

Looks like the issue is with webpack; I’d try asking this on the webpack bugtracker.

Isn’t it better to replace

const ChromiumRevision = require(path.join(helper.projectRoot(),'package.json')).puppeteer.chromium_revision;

with this code?

const ChromiumRevision = require('../package.json').puppeteer.chromium_revision;

I do believe that bundling is an important part of the Javascript ecosystem that any project should strive to support, however.

I think puppeteer could and should spend some time investigating this issue and look to put a fix / workaround in place.

@aengl @daveykane sure, let’s make pptr bundle’able. Moving this discussion over to #2119.

A try/catch & fallback to a process.env.PUPPETEER_CHROMIUM_VERSION ( per puppeteer/install.js design ) would be perfect

@131 I’m not sure I follow this.

Hi all ! Do you get a workaround for this one ? Can’t build a lambda with webpack including puppeteer…

@aengl suggested changes work, I think puppeteer could and should spend some time investigating this issue and look to put a fix / workaround in place.

How could this be closed ? We can’t bundle puppeteer (with browserify) for the same reasons - this is a major drawback.

A try/catch & fallback to a process.env.PUPPETEER_CHROMIUM_VERSION ( per puppeteer/install.js design ) would be perfect

@aslushnikov You still can help at least to highlighting this issue like unresolved and stay active discussion about that. Maybe someone find workaround or solution for this unresolved issue in this or another therd, or if webpack team fix it you can add solution to this opened thread to help other developers solve their problem

  • We close issues when they are no longer “actionable”: Puppeteer core team has no plans regarding it.
  • Feel free to continue discussion in the closed issue, that’s pretty common.

Please file the issue downstream to WebPack repository and post a link here. This way all newcomers will know where to post concerns and further details.

@aslushnikov You still can help at least to highlighting this issue like unresolved and stay active discussion about that. Maybe someone find workaround or solution for this unresolved issue in this or another therd, or if webpack team fix it you can add solution to this opened thread to help other developers solve their problem. Also will be nice if you can link webpack issue with this thread. Does it make sense to you?

same problem here