Uncaught ReferenceError: require is not defined in nodeJs

I know that there are many questions already with this error message, but I have tried all of them and it doesn’t work for me.

I want to use websockets to create a server to build an online multiplayer game (so in browser). I know that socket.io can be used
I have two tutorials that I would like to follow : https://github.com/hnasr/javascript_playground/tree/master/websocket-cell-game and https://github.com/HungryTurtleCode/multiplayerSnake
They are using express and http for the first one and socket.io for the second, but whatever I do I get this error : Uncaught ReferenceError: require is not defined. I mostly tried the solutions in this link : Client on Node.js: Uncaught ReferenceError: require is not defined but without any success. I would like to try the solution using by Peter Mortensen but I don’t know how it should be done.

It has been a week since I am on this error and I still didn’t find a solution that works for me. I wanted to try import but the examples I followed they are using import as follows:

    import { createServer } from "http";
    import { Server } from "socket.io";

But I get the error : "http". Relative references must start with either "/", "./", or "../"
I can solve the socket.io by using this link : https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.5.1/socket.io.js instead of “socket.io”, but I didn’t find it for http.
I tried with another link (https://cdnjs.cloudflare.com/ajax/libs/http-client/4.3.1/http-client.min.js) but it gave me another error so I guess it wasn’t the good way to solve it.

I also tried requirejs but to use it also uses require so it doesn’t work as well (cf this link : https://gist.github.com/guerrerocarlos/3651490)

I don’t know what more I can do…

Here is the last version of my trials

in my index.php :

<script data-main="scripts/main" src="scripts/require.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.5.1/socket.io.js" integrity="sha512-9mpsATI0KClwt+xVZfbcf2lJ8IFBAwsubJ6mI3rtULwyM3fBmQFzj0It4tGqxLOGQwGfJdk/G+fANnxfq9/cew==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="js/serverGame.js"></script>

Here is my serverGame.js

const io = require('socket.io')();

io.on ('connection', client => {
  client.emit('init', {data: 'hello world'})


And here is my package.json :

  "type": "commonjs",
  "devDependencies": {
    "autoprefixer": "^10.4.7",
    "postcss": "^8.4.14",
    "tailwindcss": "^3.1.2"
  "dependencies": {
    "colyseus.js": "^0.14.13",
    "moment": "^2.29.3",
    "requirejs": "^2.3.6",
    "socket.io": "^4.5.1",
    "ws": "^8.8.0",
    "ws-browser": "^11.4.8"
  "optionalDependencies": {
    "bufferutil": "^4.0.6",
    "utf-8-validate": "^5.0.9"

Does anyone have an idea on how to solve my problem ?
Thank you in advance