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 Warning when using ws in a webpack bundle #1126

Closed
crazyyi opened this issue May 27, 2017 · 13 comments
Closed

Module not found Warning when using ws in a webpack bundle #1126

crazyyi opened this issue May 27, 2017 · 13 comments

Comments

@crazyyi
Copy link

crazyyi commented May 27, 2017

WARNING in ./~/ws/lib/Validation.js
Module not found: Error: Can't resolve 'utf-8-validate' in '/.../node_modules/ws/lib'
 @ ./~/ws/lib/Validation.js 10:22-47
 @ ./~/ws/lib/Receiver.js
 @ ./~/ws/index.js
 @ ./src/main.js
 @ multi webpack-hot-middleware/client?reload=true ./src/main.js

WARNING in ./~/ws/lib/BufferUtil.js
Module not found: Error: Can't resolve 'bufferutil' in '/.../node_modules/ws/lib'
 @ ./~/ws/lib/BufferUtil.js 35:21-42
 @ ./~/ws/lib/Receiver.js
 @ ./~/ws/index.js
 @ ./src/main.js
 @ multi webpack-hot-middleware/client?reload=true ./src/main.js

Do I have to install bufferutil and utf-8-validate module to remove the warning? Why are these two modules separated from the main module? How do I know when I need to use them? I haven't written anything related to these two modules. I think maybe ws uses these two modules.

@crazyyi
Copy link
Author

crazyyi commented May 27, 2017

I think it is because I "require" the ws module in the browser code. But I hope the project example should point it out that ws module is not required on the client side.

@lpinca
Copy link
Member

lpinca commented May 27, 2017

This is a recurring issue, we already have a note at the beginning of the README but it seems it is not sufficient. ws does not work in the browser, the native WebSocket object must be used instead. See #1117 (comment).

@lpinca
Copy link
Member

lpinca commented May 28, 2017

Closing, feel free to continue discussing on the closed thread.

@lpinca lpinca closed this as completed May 28, 2017
@dalen
Copy link

dalen commented Nov 8, 2018

Getting the same issue when using serverless-webpack to package a AWS Lambda that includes ws through @slack/client. Webpack tries to resolve all the modules and package it up into a small file, but it fails as ws includes devDependencies in the files.

@chengjia0807
Copy link

any solution on this?

@eric-burel
Copy link

Hi, I get this issue in the main process of an Electron app. This is a Node process, not a client process.

@hrmoller
Copy link

@dalen Did you find a solution? I have the exact same case as you.

@dalen
Copy link

dalen commented Mar 25, 2019

@hrmoller yes, I added bufferutil and utf-8-validate as externals in the webpack config: https://github.com/fishbrain/codebuild-slack-notifier/blob/master/webpack.config.js#L23-L24

Works as a workaround for now at least.

@vjau
Copy link

vjau commented May 20, 2020

The problem is still there on Electron main thread (== node code, not browser).
I have implemented the fix from @dalen with success.
I post it there since his his gist is 404

// in webpack.config.js
externals: {
      bufferutil: "bufferutil",
      "utf-8-validate": "utf-8-validate",
    }

@regnaio
Copy link

regnaio commented Dec 9, 2020

This workaround using webpack-node-externals worked for me. See the section under webpack-back.config.js

https://medium.com/code-oil/webpack-javascript-bundling-for-both-front-end-and-back-end-b95f1b429810

@n42k
Copy link

n42k commented Oct 12, 2021

const WebSocketServer = __non_webpack_require__('ws').Server; worked for me.

@pranavwani
Copy link

This workaround using webpack-node-externals worked for me. See the section under webpack-back.config.js

https://medium.com/code-oil/webpack-javascript-bundling-for-both-front-end-and-back-end-b95f1b429810

Actually works thanks

@ernestostifano
Copy link

ernestostifano commented Nov 3, 2024

Just some notes for anyone that might land here in the future:

  • Webpack can (and some times should/must be used to bundle non-browser apps).
  • Generally, when you decide to bundle your back-end/server code (lambdas, scalable containers, etc.) you want the resulting bundle to be self-contained and ready to run (no need to install, no need for any node_modules).
  • In such cases, using Webpack's externals or anything similar, is not correct.
  • To properly solve this issue in these cases, you should manually install bufferutil and utf-8-validate so Webpack can find them and bundle them properly (they will be tree-shaked if not needed).

Note for ws maintainers (@lpinca):

  • Using Webpack or other bundlers for NodeJS is becoming popular (it brings lots of benefits in modern development).
  • We should figure out a way of making those modules opt-in without breaking webpack builds. At least we should document this.
  • I am open to help (right now I am in a bit of a rush, but I think there are different ways we can improve this).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests