Home > database >  Uncaught ReferenceError: require is not defined in nodeJs
Uncaught ReferenceError: require is not defined in nodeJs

Time:06-24

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 ? Thank you in advance

CodePudding user response:

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.

  • Related