简体   繁体   中英

Cannot use import statement outside a module, when using script with html

I want to make app using socket.io, i watched tutorial and then tried doing it myself. Server part works normal, and client part also, but when i tried to make html and connect client to server this error shows up in console: Cannot use import statement outside a module . I tried everything - adding type:module in package.json, renaiming script to script.mjs and using require() instead of import . Require also gives error: require is not defined . Here is client.js:

//const io = require("socket.io-client") - not working
import io from 'socket.io-client'

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

socket.on('message',message =>{
    console.log(message)
})

Thanks!

maybe you forgot to add "type": "module" on your package.json file. for more details, please take a look at this link "Uncaught SyntaxError: Cannot use import statement outside a module" when importing ECMAScript 6 , thank you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM