0

I know that there are many questions already with this error message, but I have tried all the solutions suggested but 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 for browser I have this tutorial that I would like to follow : https://github.com/HungryTurtleCode/multiplayerSnake He is using socket.io for his game, 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'})
})

io.listen(3000);

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?

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40

1 Answers1

1

You need to run your Node.js code using Node.js.

Linking to it with a <script> element and trying to run it using a web browser won't work. Web browsers are not Node.js.

Web browsers do not have features to support running servers. Web browsers do not support CommonJS modules. Web browsers do not support Node.js style module resolution where they search a node_modules directory when you import from a name instead of a URL.

You need to use Node.js.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Thank you for your answer So if I am understanding well, even if I use module for browser, as long as I am using nodeJS it won't be possible to have it work on browser ? Is there a solution for me to use websockets on my browser ? – chubby marschmallow Jun 23 '22 at 14:08
  • Websockets are an API to communicate between clients and servers. Browsers have native APIs that implement websocket clients. You need a server to communicate with. You can't write a websocket server in a browser. – Quentin Jun 23 '22 at 14:10
  • Then how is it possible for browser game to work ? (skribbl.io, browserquest.io...) ? – chubby marschmallow Jun 23 '22 at 14:12
  • Umm. Do you have some reason to think that those games don't communicate with a server? – Quentin Jun 23 '22 at 14:13
  • No of course they do, but how ? browserquest has an open source code and he is using ws but it works on browser which makes me very confused. It is my first time dealing servers so I'm sorry if my questions sound silly – chubby marschmallow Jun 23 '22 at 14:16
  • @chubbymarschmallow — As I said. Web Sockets are an API to communicate between clients and servers. So of course bits of it run in the browser. The server bits do not. The code you have written is for running a server and won't run in a browser. – Quentin Jun 23 '22 at 14:19