Socket.IO - require is not defined

2019-02-02 09:34发布

I'm trying to get socket.io working but now in Chrome I get the error:

Uncaught ReferenceError: require is not defined

client.php:9Uncaught ReferenceError: io is not defined

I changed the way to include the socket.io.js file because it dosnt exists somewher else:

<script src="/node_modules/socket.io/lib/socket.io.js"></script>

If I try

<script src="/socket.io/socket.io.js"></script>

I get: Failed to load resource: the server responded with a status of 404 (Not Found)

This is on Ubuntu with the latest of everything

I'm using the server code from http://socket.io/ to work with in the same folder like client.php and that works, named server.js, only modified port.

8条回答
The star\"
2楼-- · 2019-02-02 09:49

If your script isn't coming from your webserver, this will not work:

<script src="/socket.io/socket.io.js"></script>

You have to explicitely state host and port:

<script src="http://localhost:<port>/socket.io/socket.io.js"></script>
查看更多
你好瞎i
3楼-- · 2019-02-02 09:53

Are you running node.js along PHP on your server?

There are two "socket.io" packages, a server and a client. You're trying to load the server one (/node_modules/socket.io/lib/socket.io.js) in the browser. The script you want is called socket.io-client and you can find it at https://raw.github.com/LearnBoost/socket.io-client/master/socket.io-client.js

What happens is that socket.io automatically serves the /socket.io/socket.io.js (the client one) file from port 80 when you're running node on port 80. In your case Apache is already at port 80, so you need to serve the file from it manually.

查看更多
smile是对你的礼貌
4楼-- · 2019-02-02 09:53

Your library include (/socket.io/socket.io.js) in your client browser coding was probably OK. But you may be pointing to the wrong copy of socket.io.js If you installed socket.io using (npm install socket.io) then you might want to look in the following directory for the client version of socket.io.js:

C:\Program Files (x86)\nodejs\node_modules\socket.io\node_modules\socket.io-client\dist\socket.io.js

If its there, then you may want to copy that module to your web publishing directory, OR as an alternative, you could change the physical path that your virtual directory points to.

查看更多
Deceive 欺骗
5楼-- · 2019-02-02 10:01

Actually I was successful in getting Apache to serve up these files, today in fact.

Simply create a copy (I actually copied the files, soft links might be ok I did not try) of the contents of node_modules/socket.io/node_modules/socket.io-client/dist to your web root. For my install of socket.io (0.9.16) where I installed socket.io globally these two commands did the trick:

sudo cp -av /usr/lib/node_modules/socket.io/node_modules/socket.io-client/dist /var/www/socket.io 
chown -R www-data:www-data /var/www/socket.io

This was on a Ubuntu 13.10 box the usual LAMP stack installed before I then added node.js.

Now keeping this copy in sync as new versions of socket.io are released is a problem I have not yet tried to address yet.

Cheers

查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-02-02 10:02

If you see something like require() is not defined, it may be because the JavaScript library you're using is in the format of an AMD (Asynchronous Module Definition). Try using RequireJS (full-featured) or the curl JS library (smaller, simpler) to load SocketIO instead.

查看更多
Ridiculous、
7楼-- · 2019-02-02 10:07

I had the same problem, and I confirm that @thejh's solution worked. I however was unsure as to "what" was the server when I read his recommendation. I do have MAMP running on port 80. And the code below would be the "node.js server", which runs on port 8001.

Once I started the node.js server (ran the code below), a visit to http://localhost:4001/socket.io/socket.io.js returns the javascript file.

/* Node.js server */

var sys  = require('sys');
var io   = require('socket.io');
var http = require('http')
server = http.createServer(function(req, res) { 
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.write('http server started');
  res.end(); 
});
server.listen(4001);


// socket.io 
var socket = io.listen(server); 
socket.on('connection', function(client) {

  client.on('message', function(message) {
    // We're in!
    console.log('received client message '+ message);
  }); 
  client.on('disconnect', function() {});
  socket.send('welcome to the local node.js server!');
});

For those within Drupal, a drupal_add_js('http://localhost:4001/socket.io/socket.io.js', array('type' => 'external', 'group' => JS_LIBRARY)); within a hook_init does the trick.

查看更多
登录 后发表回答