ReferenceError: io 未定义

Posted

技术标签:

【中文标题】ReferenceError: io 未定义【英文标题】:ReferenceError: io is not defined 【发布时间】:2020-12-22 19:34:37 【问题描述】:

当我尝试使用 nodemon 在 nodeJS 服务器上运行它时,我的 javascript 文件中出现此错误。

在对象。 (C:\Users\Chaitanya\Desktop\appDir\Chiku\src\app\departmentlist\client.js:4:16) 在 Module._compile (internal/modules/cjs/loader.js:1137:30) 在 Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10) 在 Module.load (internal/modules/cjs/loader.js:985:32) 在 Function.Module._load (internal/modules/cjs/loader.js:878:14) 在 Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) 在内部/main/run_main_module.js:17:47 [nodemon] 应用程序崩溃 - 启动前等待文件更改...

我什至安装了 socket io 库使用 npm install socket.io-client --save

// const io = window.io = require('socket.io-client');


const socket = io('http://localhost:4200');

const form = document.getElementById('send-container');
const messageInput = document.getElementById('messageInp')
const messageContainer = document.querySelector(".container")

var audio = new Audio('sms_notification.mp3')

const append = (message, position)=>   //jo naya join hoga uski notification aaegi in the form of message
    const messageElement = document.createElement('div');  
    messageElement.innerText = message;
    messageElement.classList.add('message'); //show message 
    messageElement.classList.add(position);  // show new user's position(left,right)
    messageContainer.append(messageElement);
    if(position =='left')
    audio.play();
    

form.addEventListener('submit', (e)=>
    e.preventDefault();
    const message = messageInput.value;
    append(`You: $message`, 'right')
    socket.emit('send', message);
    messageInput.value = ''
)
 const name = prompt("Enter your name to join");
socket.emit('new-user-joined', name)  // new name will be accepted and executed by socket.on and will be displayed

socket.on('user-joined', name=>
    append(`$name joined the chat`, 'right')
)

socket.on('recieve', data=>
    append(`$data.name: $data.message`, 'left')
)

socket.on('left', name=>
    append(`$name left the chat`, 'left')
)
body
    background-image: url("abc.jpg") ;


.logo
display: block;
margin: auto;
width: 75px;
height: 75px;


h1
    position: absolute;
    margin-top: 12px;
    font-size: 40px;
    font-family: Georgia, 'Times New Roman', Times, serif;
    font-style: inherit;
    text-align: center;
    color:whitesmoke; 
    font-stretch: condensed;


.container12
    max-width: 955px;
    background-color:whitesmoke;
    margin: auto;
    height: 60vh;
    padding: 33px;
    overflow-y: auto;
    margin-bottom: 23px;


.message
    background-color:skyblue;
    width: 24%;
    padding: 10px;
    margin: 17px 12px;
    border: 2px solid burlywood;
    border-radius: 10px;
    font-family: cursive;


.left
    float: left;
    clear: both;


.right
    float: right;
    clear: both;


#send-container
    display: block;
    margin: auto;
    text-align: center;
    max-width: 985px;
    width: 100%;


#messageInp
    width: 100%;
    border: 2px solid black;
    border-radius: 6px;
    height: 34px;


.btn
    cursor: pointer ;
    border: 2px solid black;
    border-radius: 6px;
    height: 34px;
<!DOCTYPE html>
<html lang = "en">
 <head>
     <meta charset="UTF-8">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <title>Gap-Shap -Online chatting</title>
     <script defer src="http://localhost:4200/socket.io/socket.io.js"></script>   
     <script defer src="client.js"></script>
     <script scr="/socket.io/socket.io.js"></script>
     
     <link rel="stylesheet" href="departmentlist.component.css">
     <link rel="icon" type="image/jpg" href=".\chat.jpg">
     
 </head>   
 <body>


    
    
    <nav>
        <h1>Gap-Shap Online Chatting</h1>


        <img class="logo" src="chat.jpg">
    </nav>
    <div class="container12">
        
    </div>
    <div class="send">
        <form action="#" id="send-container">
            <input type="text" name="messageInp" id="messageInp">
            <button class="btn" type="submit">Send</button>
        </form>    
    </div>
    

    
 </body>
</html>

【问题讨论】:

嗯,你把 io 导入注释掉了……所以那当然没有定义? 【参考方案1】:

你说你正在使用 nodemon,所以这是一个 nodeJS 脚本。您必须先导入 socketIO。这通常位于文件的开头,

const io = require('package-name');

【讨论】:

我试过这个。它不起作用。也许我的 node 或 nodemon 版本有问题,因为它无法识别 socket.io

以上是关于ReferenceError: io 未定义的主要内容,如果未能解决你的问题,请参考以下文章

ReferenceError: io 未定义

socket.io - ReferenceError:未定义套接字

ReferenceError:未定义套接字。套接字.io

ReferenceError:元素未定义。当我尝试运行 ios 测试时遇到此问题,以 jest 作为测试运行器反应本机

获取 ReferenceError:在 react-native ios 上运行 detox 测试时未定义设备

Angular AOT 和汇总 - 未捕获的 ReferenceError:未定义导出