Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module not found error can't resolve 'ws' in puppeteer-core/lib #3466

Closed
kamira opened this issue Oct 30, 2018 · 22 comments
Closed

Module not found error can't resolve 'ws' in puppeteer-core/lib #3466

kamira opened this issue Oct 30, 2018 · 22 comments

Comments

@kamira
Copy link

@kamira kamira commented Oct 30, 2018

  • Puppeteer version: 1.9.0
  • Platform / OS version: Oracle Linux 7
  • Node.js version: 8.11.4

What steps will reproduce the problem?

const puppeteer = require("puppeteer-core");
    (async () => {
      const browser = await puppeteer.launch({
        executablePath: '../../chrome/chrome'
      });
      const page = await browser.newPage();
      await page.goto(DashboardConstants.LANDING_PAGE_PATH + "&embed=true");
      await page.pdf({path: "example.pdf", fullPage: true, format: "A4"});

      await browser.close();
    })();
  }
  1. git clone kibana
  2. git checkout v6.4.2
  3. add puppeteer-core in package.json
  4. download chrome and extract at src/core_plugins/kibana/
  5. edit src/core_plugins/kibana/public/dashboard/dashboard_app.js with code

What is the expected result?

I should Pass and kibana will be running

What happens instead?

I got Module not found error message.

Error Message:

error  [05:44:03.811] [fatal] Error: Optimizations failure.
8270 modules
ERROR in ./node_modules/puppeteer-core/lib/WebSocketTransport.js
Module not found: Error: Can't resolve 'ws' in '/home/ELK/kibana-test/kibana/node_modules/puppeteer-core/lib'

at FsOptimizer.failedStatsToError (/home/ELK/kibana-test/kibana/src/optimize/base_optimizer.js:429:17)
at compiler.run (/home/ELK/kibana-test/kibana/src/optimize/fs_optimizer.js:36:26)
at onCompiled (/home/ELK/kibana-test/kibana/node_modules/webpack/lib/Compiler.js:241:12)
at applyPluginsAsync.err (/home/ELK/kibana-test/kibana/node_modules/webpack/lib/Compiler.js:514:14)
at next (/home/ELK/kibana-test/kibana/node_modules/tapable/lib/Tapable.js:202:11)
at Compiler.<anonymous> (/home/ELK/kibana-test/kibana/node_modules/webpack/lib/CachePlugin.js:78:5)
at Compiler.applyPluginsAsyncSeries (/home/ELK/kibana-test/kibana/node_modules/tapable/lib/Tapable.js:206:13)
at compilation.seal.err (/home/ELK/kibana-test/kibana/node_modules/webpack/lib/Compiler.js:511:11)
at Compilation.applyPluginsAsyncSeries (/home/ELK/kibana-test/kibana/node_modules/tapable/lib/Tapable.js:195:46)
at self.applyPluginsAsync.err (/home/ELK/kibana-test/kibana/node_modules/webpack/lib/Compilation.js:671:19)
at Compilation.applyPluginsAsyncSeries (/home/ELK/kibana-test/kibana/node_modules/tapable/lib/Tapable.js:195:46)
at self.applyPluginsAsync.err (/home/ELK/kibana-test/kibana/node_modules/webpack/lib/Compilation.js:662:11)
at next (/home/ELK/kibana-test/kibana/node_modules/tapable/lib/Tapable.js:202:11)
at /home/ELK/kibana-test/kibana/node_modules/uglifyjs-webpack-plugin/dist/index.js:282:11
at step (/home/ELK/kibana-test/kibana/node_modules/uglifyjs-webpack-plugin/dist/uglify/index.js:90:11)
at done (/home/ELK/kibana-test/kibana/node_modules/uglifyjs-webpack-plugin/dist/uglify/index.js:99:22)
at /home/ELK/kibana-test/kibana/node_modules/uglifyjs-webpack-plugin/dist/uglify/index.js:105:15
at /home/ELK/kibana-test/kibana/node_modules/worker-farm/lib/farm.js:191:19
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
@kamira kamira changed the title Module not found error Module not found error can't resolve 'ws' in puppeteer-core/lib Oct 30, 2018
@JoelEinbinder

This comment has been minimized.

Copy link
Collaborator

@JoelEinbinder JoelEinbinder commented Oct 31, 2018

Kibana looks like they have quite an extensive dependency system on top of just package.json. I'd file the issue there. puppeteer-core definitely depends on ws and it should be installed when you add it.

@aslushnikov aslushnikov closed this Nov 1, 2018
@AirborneEagle

This comment has been minimized.

Copy link

@AirborneEagle AirborneEagle commented Nov 26, 2018

I am having the same issue,
But I am not sure I understand what the fix was?
I ran this, but it did not change anything.
npm install --save ws

What was done to solve the OP problem?

@aslushnikov

This comment has been minimized.

Copy link
Collaborator

@aslushnikov aslushnikov commented Nov 26, 2018

@AirborneEagle this seems to be a downstream issue, please file a bug to kibana

@AirborneEagle

This comment has been minimized.

Copy link

@AirborneEagle AirborneEagle commented Nov 26, 2018

I am confused. I am not using Kibana.
I am using puppeteer in my own application.
More specifically I am using pdf-puppeteer.
https://github.com/westmonroe/pdf-puppeteer#readme

@AnthoniG

This comment has been minimized.

Copy link

@AnthoniG AnthoniG commented Mar 4, 2019

Did this ever get resolved?
I am getting the same error, either if I use puppeteer or puppeteer-core
I am using inside Electron if that makes any difference but it shouldn't.

Help ?

@tomermes

This comment has been minimized.

Copy link

@tomermes tomermes commented Mar 9, 2019

Did this ever get resolved?
I am getting the same error, either if I use puppeteer or puppeteer-core
I am using inside Electron if that makes any difference but it shouldn't.

Help ?

It happened to me when I accidentally tried to ran puppeteer from inside a browser code.
If that's your case by any chance.

@AnthoniG

This comment has been minimized.

Copy link

@AnthoniG AnthoniG commented Mar 9, 2019

@tomermes Yea it's similar. Electron acts like a browser in many ways, because it uses Chromium etc under the hood.

Tried looking around and it seems that you can not use Puppeteer inside Electron 😞

@aslushnikov

This comment has been minimized.

Copy link
Collaborator

@aslushnikov aslushnikov commented Mar 15, 2019

Tried looking around and it seems that you can not use Puppeteer inside Electron

@AnthoniG Electron has a lot of powerful APIs on its own. Why would you want to?

@rmondesilva

This comment has been minimized.

Copy link

@rmondesilva rmondesilva commented Apr 3, 2019

I am getting the same error while compiling.

ERROR in ./node_modules/puppeteer/lib/WebSocketTransport.js
Module not found: Error: Can't resolve 'ws' in 'c:\...\project\node_modules\puppeteer\lib'

How to fix this? I checked the path, it exist.

@AnthoniG

This comment has been minimized.

Copy link

@AnthoniG AnthoniG commented Apr 4, 2019

@aslushnikov Electron is very powerful yes. However in order to automate scraping simple websites I have to use IPC and it gets very messy very quickly.

Using puppeteer sandbox I could do it in a few less lines and there was no messing about with IPC at all. So having this inside Electron, or if there's anything similar you might now, would be absolutely great.

@kalimuthu123

This comment has been minimized.

Copy link

@kalimuthu123 kalimuthu123 commented May 7, 2019

I am getting the same error while compiling.

ERROR in ./node_modules/puppeteer/lib/WebSocketTransport.js
Module not found: Error: Can't resolve 'ws' in 'c:\...\project\node_modules\puppeteer\lib'

How to fix this? I checked the path, it exist.

I also tried the same bug when i used to webpack it does not work for me

@stychu

This comment has been minimized.

Copy link

@stychu stychu commented Jun 18, 2019

Having the same trouble. I want to run puppeteer in the context of the electron. So when I run script with pupeeter scraping like node test.js its working. But when I require it inside the electron app to be able to run this script on button click from within electron app I get

./node_modules/puppeteer-core/lib/WebSocketTransport.js
Module not found: Can't resolve 'ws' in .....
@AmmarHalees

This comment has been minimized.

Copy link

@AmmarHalees AmmarHalees commented Jul 11, 2019

Guys, if you're attempting to use it from the create-react-app bundle, then it's not going to work.
According to their npm page:

https://www.npmjs.com/package/ws

Note:

This module does not work in the browser. The client in the docs is a reference to a back end with the role of a client in the WebSocket communication. Browser clients must use the native WebSocket object. To make the same code work seamlessly on Node.js and the browser, you can use one of the many wrappers available on npm, like isomorphic-ws.

@aslushnikov

This comment has been minimized.

Copy link
Collaborator

@aslushnikov aslushnikov commented Jul 15, 2019

Also, for bundling Puppeteer for browser use, see https://github.com/GoogleChrome/puppeteer/blob/master/utils/browser/README.md

@dfoverdx

This comment has been minimized.

Copy link

@dfoverdx dfoverdx commented Jul 20, 2019

For me, this happened when bundling with webpack, intending the bundle to be used in node. The trick was to set puppeteer as an external.

const config = {
    // ...
    externals: {
        puppeteer: 'require("puppeteer")',
        // ...
    },
};

Wrapping it in require() is important because puppeteer doesn't have a global export like jQuery and React do.

@9oelM

This comment has been minimized.

Copy link

@9oelM 9oelM commented Jul 28, 2019

@dfoverdx +1 for your solution. I was using webpack too (it's not a web application tho, it's just running on node) and it definitely worked for me.

My initial error output was as follows:

$ webpack && node dist/bundle.js
Hash: 1b2fd47f36807b1b273d
Version: webpack 4.38.0
Time: 1573ms
Built at: 07/28/2019 10:10:40 PM
    Asset      Size  Chunks             Chunk Names
bundle.js  1.53 MiB    main  [emitted]  main
Entrypoint main = bundle.js
[0] fs (ignored) 15 bytes {main} [built]
[1] ./BrowserFetcher (ignored) 15 bytes {main} [built]
[2] util (ignored) 15 bytes {main} [built]
[3] util (ignored) 15 bytes {main} [built]
[4] rimraf (ignored) 15 bytes {main} [built]
[5] child_process (ignored) 15 bytes {main} [built]
[6] readline (ignored) 15 bytes {main} [built]
[7] ./node6/lib/Puppeteer (ignored) 15 bytes {main} [built]
[./node_modules/webpack/buildin/global.js] (webpack)/buildin/global.js 472 bytes {main} [built]
[./node_modules/webpack/buildin/module.js] (webpack)/buildin/module.js 497 bytes {main} [built]
[./src/index.ts] 3.25 KiB {main} [built]
    + 78 hidden modules

ERROR in ./node_modules/puppeteer/lib/WebSocketTransport.js
Module not found: Error: Can't resolve 'ws' in '/Users/jm/Documents/Joel/scraper/node_modules/puppeteer/lib'
 @ ./node_modules/puppeteer/lib/WebSocketTransport.js 16:18-31
 @ ./node_modules/puppeteer/lib/Launcher.js
 @ ./node_modules/puppeteer/lib/Puppeteer.js
 @ ./node_modules/puppeteer/index.js
 @ ./src/index.ts
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

But when I just added

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

to webpack config, it worked like a charm:

$ webpack && node dist/bundle.js
Hash: 1bd4c72ee57858351a97
Version: webpack 4.38.0
Time: 1117ms
Built at: 07/28/2019 10:26:41 PM
    Asset      Size  Chunks             Chunk Names
bundle.js  17.7 KiB    main  [emitted]  main
Entrypoint main = bundle.js
[./src/index.ts] 3.25 KiB {main} [built]
[puppeteer] external "require(\"puppeteer\")" 42 bytes {main} [built]
✨  Done in 5.34s.
@shubuu97

This comment has been minimized.

Copy link

@shubuu97 shubuu97 commented Aug 2, 2019

For me, this happened when bundling with webpack, intending the bundle to be used in node. The trick was to set puppeteer as an external.

const config = {
    // ...
    externals: {
        puppeteer: 'require("puppeteer")',
        // ...
    },
};

Wrapping it in require() is important because puppeteer doesn't have a global export like jQuery and React do.

For me, this happened when bundling with webpack, intending the bundle to be used in node. The trick was to set puppeteer as an external.

const config = {
    // ...
    externals: {
        puppeteer: 'require("puppeteer")',
        // ...
    },
};

Wrapping it in require() is important because puppeteer doesn't have a global export like jQuery and React do.

Can anyone tell me where to add this config?

@9oelM

This comment has been minimized.

Copy link

@9oelM 9oelM commented Aug 2, 2019

@shubuu97
Inside webpack.config.js. Here's my config as an example:

const path = require('path');

module.exports = {
  entry: './src/index.ts',
  devtool: 'inline-source-map',
  mode: 'development',
  // the webpack config just works
  // SEE https://github.com/webpack/webpack/issues/1599
  target: 'node',
  node: {
    __dirname: false,
    __filename: false,
  },
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: 'ts-loader',
        exclude: /node_modules/,
      },
      {
        test: /\.ts$/,
        loader: 'tslint-loader',
        enforce: 'pre',
        options: {
          fix: true,
          tsConfigFile: 'tsconfig.json',
        },
      },
    ],
  },
  resolve: {
    extensions: ['.tsx', '.ts', '.js'],
  },
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
  externals: {
    puppeteer: 'require("puppeteer")',
    fs: 'require("fs")',
  },
};
@shubuu97

This comment has been minimized.

Copy link

@shubuu97 shubuu97 commented Aug 2, 2019

Do we need to do anything else after adding this because i'm still facing the same error

@kalimuthu123

This comment has been minimized.

Copy link

@kalimuthu123 kalimuthu123 commented Aug 5, 2019

i got an error we require is not defined

@dfoverdx

This comment has been minimized.

Copy link

@dfoverdx dfoverdx commented Aug 7, 2019

i got an error we require is not defined

@kalimuthu123 what environment are you running it in? My solution was for use in Node. Browsers would not have require defined.

I know I saw documentation once for running it in the browser, but I can't seem to find it now. Since you don't need to download Chromium for use in the browser, you should use puppeteer-core.

@Nisthar

This comment has been minimized.

Copy link

@Nisthar Nisthar commented Sep 27, 2019

@dfoverdx I followed the guide for building puppeteer-web.js

I have this code inside background.js file

const puppeteer = require('puppeteer');
console.log(puppeteer);

I am getting

Uncaught ReferenceError: puppeteer is not defined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
You can’t perform that action at this time.