-1

I installed socket.io-client dependency and when I tried to import it it gave me this error:

SyntaxError: Unexpected token '{'. import call expects exactly one argument

Here is the code:

import { io } from "socket.io-client";

const socket = io("http://localhost:3000");

function createNewMessage() {
  const getInputById = document.getElementById("message_input");
  const createSpan = document.createElement("p");
  createSpan.innerHTML = getInputById.value;
  createSpan.className = "message";
  getInputById.value = "";
  document
    .getElementsByClassName("messanger_main_container")[0]
    .appendChild(createSpan);
}

Any suggestions please?

user14587589
  • 449
  • 3
  • 19
  • 1
    Are you running this with Node.JS? Which version? – Quentin Jun 01 '21 at 07:57
  • if `es6` is not supported, you should use like `const io = require("socket.io-client");` – Hagai Harari Jun 01 '21 at 07:59
  • 1
    That's a very odd error message, I would have expected something like "`import` statements can only be used in modules." It seems to think that your `import` is the `import` pseudo-function from dynamic import, but it isn't, it's a correctly-formatted static `import` statement. How are you running the code? In a browser? If so, do you have `type="module"` on the `script` for the code? – T.J. Crowder Jun 01 '21 at 07:59
  • I tried using require but it gave error that require is not defined – user14587589 Jun 01 '21 at 08:00
  • @T.J.Crowder I am actually importing it as a script in my html. I installed it and I am trying to import it from node modules – user14587589 Jun 01 '21 at 08:01
  • You may need to use a bundler like Webpack or Rollup. – Ari Seyhun Jun 01 '21 at 08:02
  • @Acidic9 Okay I will try to set up webpack – user14587589 Jun 01 '21 at 08:02
  • What do you mean by *" I am actually importing it as a script in my html."*? Remember there are two completely separate environments here, the code running on the server (which may be using Node.js) and the code running on the browser (which isn't, it's running in a browser). – T.J. Crowder Jun 01 '21 at 08:03
  • I am running it in the browser – user14587589 Jun 01 '21 at 08:03

1 Answers1

-1

Try importing like this

import * as io from 'socket.io-client';

Refer this for more info.

Raj Kumar
  • 9
  • 2