How I can get require() to work in node.js

when I inspect the browser am getting the error that say " Uncaught ReferenceError: require is not defined
"
Am trying to make socket.io work on my web server…any help on how I can make that work…

I will be very happy for any step by step on how to set up node.js socket.io

Thanks.

Hi,

You’re probably seeing this error, as require() does not exist in the browser/client-side JavaScript.
If you want to use require() in the browser, then you need to use something like require.js.

If that doesn’t answer your question, could you please provide enough code to recreate the problem.

thanks for your respond Pullo…I have try to use require.js but still having problem with it but I guess there is something that is not right yet… I downloaded the script and link it to my document and still getting this error
" Uncaught Error: Module name “socket.io-client” has not been loaded yet for context: _. Use require()
http://requirejs.org/docs/errors.html#notloaded "

How can I fit this

Hi,

It’s hard to say without enough code to reproduce your issue : )

This is my html code. I want to use the socket.io on my web server which I create package.json inside the root folder of the project am working on. Below is how I link to the socket.io in the html. the Module name ““socket.io-client”” is inside socket.io.js. socket.io.js code is the code generate when I create dependencies with my package.json. Dont know what is wrong how I can go about it…

<!DOCTYPE html>
<html>
<head>
<title>Very simple WebRTC application with a Node.js signalling server</title>
</head>
<body>

<script src='js/require.js'></script>
<script src='node_modules/socket.io/lib/socket.io.js'></script>
<script src='js/lib/adapter.js'></script>
<script src='js/main.js'></script>
</body>
</html>

the generated socket.io code

/**
 * Module dependencies.
 */

var client = require('socket.io-client');

/**
 * Version.
 */

exports.version = '0.9.16';

/**
 * Supported protocol version.
 */

exports.protocol = 1;

/**
 * Client that we serve.
 */

exports.clientVersion = client.version;

/**
 * Attaches a manager
 *
 * @param {HTTPServer/Number} a HTTP/S server or a port number to listen on.
 * @param {Object} opts to be passed to Manager and/or http server
 * @param {Function} callback if a port is supplied
 * @api public
 */

exports.listen = function (server, options, fn) {
  if ('function' == typeof server) {
    console.warn('Socket.IO\'s `listen()` method expects an `http.Server` instance\n'
    + 'as its first parameter. Are you migrating from Express 2.x to 3.x?\n'
    + 'If so, check out the "Socket.IO compatibility" section at:\n'
    + 'https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x');
  }

  if ('function' == typeof options) {
    fn = options;
    options = {};
  }

  if ('undefined' == typeof server) {
    // create a server that listens on port 80
    server = 80;
  }

  if ('number' == typeof server) {
    // if a port number is passed
    var port = server;

    if (options && options.key)
      server = require('https').createServer(options);
    else
      server = require('http').createServer();

    // default response
    server.on('request', function (req, res) {
      res.writeHead(200);
      res.end('Welcome to socket.io.');
    });

    server.listen(port, fn);
  }

  // otherwise assume a http/s server
  return new exports.Manager(server, options);
};

/**
 * Manager constructor.
 *
 * @api public
 */

exports.Manager = require('./manager');

/**
 * Transport constructor.
 *
 * @api public
 */

exports.Transport = require('./transport');

/**
 * Socket constructor.
 *
 * @api public
 */

exports.Socket = require('./socket');

/**
 * Static constructor.
 *
 * @api public
 */

exports.Static = require('./static');

/**
 * Store constructor.
 *
 * @api public
 */

exports.Store = require('./store');

/**
 * Memory Store constructor.
 *
 * @api public
 */

exports.MemoryStore = require('./stores/memory');

/**
 * Redis Store constructor.
 *
 * @api public
 */

exports.RedisStore = require('./stores/redis');

/**
 * Parser.
 *
 * @api public
 */

exports.parser = require('./parser');

Man. this completely fell off my radar. Sorry about that.
Are you still having trouble getting it to work?

thanks @pullo I got it fix.

Good news.
What was the cause of the problem? (it might help somebody else).

I do not link the socket.io well and the web server is running on the same port that is blocking the socket.io server so I have to correct the port the web server is listen to and the one the socket.io connect to.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.