0

I've just started with node.js and socket.io. I installed node.js using the windows installer, resulting in the following install path:

C:\Program Files (x86)\nodejs

I used npm to install socket.io and managed to include this in my server file, which is located in my projects folder, under

D:\projects\node\nodeserv.js

I then included the socket.io.js, which is located under the same socket.io folder, under the nodjs folder.

However, when I try to open the HTML file containing the client code, I get an error in socket.io.js stating:

Undefined reference to 'require'

Can someone help me out and see what I am doing wrong?

Wayne
  • 59,728
  • 15
  • 131
  • 126
thomaux
  • 19,133
  • 10
  • 76
  • 103

3 Answers3

2

Make sure to check your spelling very carefully. If you can post post some code to look at, we can probably tell you the problem right away. More than likely it's a simple typo.

Michael Rice
  • 1,173
  • 5
  • 13
2

It sounds like you are trying to run node.js from the browser. Node.js runs on the server with the node executable. When you open the HTML file in your browser, it will execute the Javascript on it in a non node.js environment.

fent
  • 17,861
  • 15
  • 87
  • 91
  • I know, but shouldn't I be able to open a io.Socket from my client code? – thomaux Dec 17 '11 at 01:20
  • Read the [socket.io documentation](https://github.com/learnboost/socket.io) and note which code snippers are for the server and which are for the client. – fent Dec 17 '11 at 02:09
0

Apparently you need to get the socket.io.js file from the nodejs server.

So instead of an include, use an async call to get the file, like:

$.getScript('http://localhost:1337/socket.io/socket.io.js', function(){
    //You can now use the io namespace
});
thomaux
  • 19,133
  • 10
  • 76
  • 103