1

In an express.Js project i have socket.io installed for my server and socket.io-client for my client and the projected is bundled using webpack5.

in my client i have this code;


const { io } = require("socket.io-client");

const socket = io();

socket.on("connect", () => {
      console.log(socket.id);
});

and when i run the command to build it compiles without problems but in the browser i get the error ;

Uncaught ReferenceError: require is not defined

how can i fix this ?

i couldn't find a good answer here ; Socket.IO - require is not defined

thanks in advance.

mod7ex
  • 874
  • 9
  • 27
  • 2
    Webpack doesn't seem to be packing it. You'll need to make sure you are using the generated file from webpack and provide your webpack config to debug this. – Quentin Jan 18 '22 at 10:16
  • 2
    `import { io } from "socket.io-client"` – Vasyl Boroviak Jan 18 '22 at 10:23
  • @VasylBoroviak also this option gives the same error i already fixed the problem of require not defined but with socket it appears again – mod7ex Jan 18 '22 at 10:30

0 Answers0