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)
3/11/2012 9:38:47 PM

Accepted Answer

On the client, did you do:

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

before you set the socket variable?

9/29/2011 9:24:31 PM

Node.js newbie here! I am pretty sure this has been answered. Yet I kept finding problems with the src for the socket. Apparently this : <script src="/"> was not working for me on the client side.

I've replaced the above line with this and it seems to work fine.

<script src=""></script>

(Edit: although this is obvious, this link may not work depending on when you are reading this answer. Please pick the latest link from:

Here is a working client side code:


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

        var socket = io('http://localhost:8080');
        console.log("Socket connected"+socket.connected);

        socket.on('notification', function(value){
            //insert your code here


On the server side (handles only 1 socket)

var app  = require('express')();
var http = require('http').Server(app);
var io   = require('')(http);
var port = process.env.PORT || 8080;

app.get('/', function(req, res){
    console.log("app works");

io.on('connection', function(socket){
    socket.emit('notification', {message:"hi"});

http.listen(port, function(){
    console.log('listening on :' + port);

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow