-1

I have initiated a new project with NPM : npm init

I have installed the socket.io-client package.

package.json:

{ "name": "client", "version": "1.0.0", "description": "", "main": "script.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "socket.io-client": "^4.5.4" } }

script.js:

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

const socket = io('http://localhost:3000')

socket.on('connect', () => {

console.log('Hello - ' + socket.id)
}) 

The error I get:

npm ERR! Missing script: "start"

I have added the start command to package.json:

"start": "node script.js"

Now I get:

SyntaxError: Cannot use import statement outside a module

I have tried adding start command, and did not worked.

John
  • 33
  • 5
  • 1
    Does this answer your question? ["Uncaught SyntaxError: Cannot use import statement outside a module" when importing ECMAScript 6](https://stackoverflow.com/questions/58211880/uncaught-syntaxerror-cannot-use-import-statement-outside-a-module-when-import) – jsejcksn Jan 17 '23 at 22:42

1 Answers1

0

You can try one of these:

  • Add type="module" to whereever you import your script.
  • Add "type": "module" to your package.json file.

See more here: "Uncaught SyntaxError: Cannot use import statement outside a module" when importing ECMAScript 6

fatih.jsx
  • 90
  • 1
  • 8
  • 1
    Please don't answer duplicates. Duplicates are closed. – Thomas Sablik Jan 17 '23 at 22:43
  • @kaligouri . I have added type module into my package.json file as you wrote. Now I have an another error. In my script.js there is document.getElementById function which trying to get an element in my index.html. Now I get: ReferenceError: document is not defined. I have added my script.js into my index.html with script tag. I think now script.js does not sees my index.html, how can I fix that? I – John Jan 17 '23 at 23:06