Node server, ‘io is not defined’?


I’ve followed the exact same steps which have always previously worked for me, create application through express, place the module dependencies in the node_modules folder. It appears that the client-side javascript file isn’t being found.

(I’ve looked at other peoples fixes, which is to include the JavaScript file in a script tab. I have not had to do this for my previous node + projects).

JavaScript on client:

var socket = io.connect('http://localhost');

JavaScript on server:

var io = require('').listen(app);

node_modules folder:, which has an internal node_modules folder containing

Error Message:

Uncaught ReferenceError: io is not defined
(anonymous function)

When I include the client manually:

I get a different error which is:

Uncaught TypeError: Object #<Object> has no method 'connect'
(anonymous function)

Problem courtesy of: Jack


On the client, did you do:

<script src="/"></script>

before you set the socket variable?

Solution courtesy of: Jorge Israel Peña

View additional discussion.