Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

socket.io is getting error: "Unexpected token ..."

I am starting with socket.io, and while following their tutorial I had encountered the following error message:

/path/to/my/app/node_modules/ws/lib/websocket.js:347
      ...options
      ^^^

SyntaxError: Unexpected token ...
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:549:28)
    at Object.Module._extensions..js (module.js:586:10)
    at Module.load (module.js:494:32)
    at tryModuleLoad (module.js:453:12)
    at Function.Module._load (module.js:445:3)
    at Module.require (module.js:504:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/path/to/my/app/node_modules/ws/index.js:3:19)

This is my index.js:

var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(socket){
  console.log('a user connected');
});

http.listen(3000, function(){
  console.log('listening on *:3000');
});

Have node installed, express and socket.io. Everything worked fine until this step on the tutorial. If I remove io variable it start running again.

Searched on google for the same error, but couldnt find anything.

like image 765
Pablo Werlang Avatar asked Oct 04 '19 02:10

Pablo Werlang


People also ask

What is unexpected token in JavaScript?

Not follow them throws an error.An unexpected token occurs if JavaScript code has a missing or extra character { like, ) + – var if-else var etc}. Unexpected token is similar to syntax error but more specific.Semicolon(;) in JavaScript plays a vital role while writing a programme.

How to handle errors in sockets?

To handle errors, we can handle these events using the out-socket object that we created on our client. For example – If we have a connection that fails, we can use the following code to connect to the server again − socket.on('connect_failed', function() { document.write("Sorry, there seems to be an issue with the connection!");

What is an example of an unexpected error in JavaScript?

Example 1: It was either expecting a parameter in myFunc (mycar, ) or not, .So it was enable to execute this code. Example 2: An unexpected token ‘, ‘occurs after i=0 which javascript cannot recognize.We can remove error here by removing extra.

What is socket Io in Twitter?

~/Projects/tweets/index.js Socket.IO enables real-time, bidirectional and event-based communication. It works on every platform, browser or device, focusing equally on reliability and speed.


1 Answers

Seems like I found the answer. I will post here for other people having the same problem. After keep looking for an answer I found this link:

https://github.com/uport-project/uport-cli-client/issues/2

The user Zachferland answers the OP question about the ellipsis (...) error.

@KamesCG thanks for the issue! yeah it seems the object spread operator was not supported in node until 8.2.1 (with flag, and then 8.6.0). To try it right now, run a greater version of node, and in the future we will update uport-js-client to transpile the src for wider node version support.

Then I checked my node version:

[root@localhost test]# node -v
v6.17.1

Which answer my question. When I installed nodejs, I just run

yum install nodejs -y

Which installed the old version. Seem like you have to update yum rep. In my case I have a CentOS7 server. I think apt have the same problem.

After following this tutorial, everything worked fine.

To summarize, I ran the following, according to the link:

yum install -y gcc-c++ make
curl -sL https://rpm.nodesource.com/setup_10.x | sudo -E bash -
sudo yum install nodejs
like image 85
Pablo Werlang Avatar answered Oct 24 '22 09:10

Pablo Werlang