[英]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.我想使用 socket.io 制作应用程序,我看了教程然后尝试自己做。 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
.服务器部分正常工作,客户端部分也正常,但是当我尝试制作 html 并将客户端连接到服务器时,控制台中出现此错误: 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
.我尝试了一切 - 在 package.json 中添加type:module
,将脚本重命名为script.mjs
并使用require()
而不是import
。 Require also gives error: require is not defined
. Require 也给出错误: require is not defined
。 Here is client.js:这是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.也许您忘记在 package.json 文件中添加“类型”:“模块”。 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有关更多详细信息,请在导入 ECMAScript 6 时查看此链接“Uncaught SyntaxError: Cannot use import statement outside a module” ,谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.