1

for an assignment, I've created a chat app with Socket.io. I was able to create a functioning chat by following this tutorial: https://www.youtube.com/watch?v=tHbCkikFfDE. However, the assignment gave me a specific chat server to use and I am struggling to do so. I am trying to import io and then use the io.connect() method to connect to the server url. The problem is, when I try to import, I get a "SyntaxError: Unexpected identifier".

This is my code up until that line:

var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var users = [];
var connections = [];
import io from "socket.io-client";

The last line, 'import io from "socket.io-client";', produces the error with the carets pointing specifically to the "io" part of the statement. Any ideas on what the issue might be or suggestions for alternative approaches? If it wasn't already clear, I'm quite inexperienced with JavaScript and might not be tackling the problem in the best way.

Dylbert
  • 57
  • 1
  • 7
  • 1
    Are you trying to build with Node? You'll need to either use Babel to compile your application _or_ rename your file to `.mjs` and use the [`--experimental-modules` flag](https://nodejs.org/api/esm.html#esm_introduction). I prefer the first option – Phil Apr 29 '19 at 04:28
  • 1
    Possible duplicate of [Node.js - SyntaxError: Unexpected token import](https://stackoverflow.com/questions/39436322/node-js-syntaxerror-unexpected-token-import) – Alexander Staroselsky Apr 29 '19 at 05:10
  • line 4 and last line both have same variables. this is one another problem. one should'nt declare variables with same name in same scope since javascript will override the value of first by second afaik . though i don't think this is related to your problem **the error with the carets pointing specifically to the "io" part of the statement. **. try naming your first variable with different name and let us know – Manish Gowardipe Apr 29 '19 at 05:20

1 Answers1

1

Did you try renaming the var io = require('socket.io').listen(server); in your 3rd line of code? Might be because you are trying to use same identifiers.

Anitta Paul
  • 455
  • 4
  • 15