ws: WebSocket.Server is not a constructor
content is same with title.
I used ‘ws’ module in electron main process not browser.
It work on local environment, but occur error with message ‘WebSocket.Server is not a constructor’ when packaging.
I’m tried to solve this with add external: ['WebSocket', 'WebSocketServer'] in webpack.config.js. but It didn’t work.
anyone who can solve this problem. please, tell me how to solve.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 3
- Comments: 24 (2 by maintainers)
There is no
ws.Serverin the ESM version, useimport { WebSocketServer } from 'ws'instead.I’m getting this with ESM:
package.json:
I can confirm the solution by @pyeongoh is working fine. In my case I was using Vue with electron-builder and I had to add the following configuration options to vue.config.js:
Afterwards everything worked as expected.
try this
import WebSocket, { WebSocketServer as WSWebSocketServer } from ‘ws’;
// work with commonjs and esm const WebSocketServer = WebSocket.Server || WSWebSocketServer;
this is still an issue for me:
wsfor testing, so i have it declared as a dev-dependencyws_1.Server is not a constructor7.0.0fixes the issuepackage.json:tsconfig.json:my.test.ts:Error:
I have encountered this problem when building an electron project and other solutions did not work for me.
the message
ws does not work in the browser. Browser clients must use the nativecomes from the file ./node_modules/ws/browser.js because either electron or ws thinks you are in a browser.
the fix that will work for all is to just change the line
"browser": "browser.js",in ./node_modules/ws/package.json to"browser": "index.js",or just delete it . then the error wont show up because even if it thinks you are running in a browser it will still load the index.js main file@loicgirard The
wsserver cannot run in the browser.References: