簡體   English   中英

錯誤:嘗試運行簡單的服務器 js 文件時找不到模塊“socket.io”

[英]Error: Cannot find module 'socket.io' when trying to run a simple server js file

這是我嘗試使用node server.js運行的 server.js 文件:

var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket-io')(http); //also tried socket.io instead of socket-io

io.on('connection', (socket) => {
console.log('User Online');

    socket.on('canvas-data', (data) => {
        socket.broadcast.emit('canvas-data', data);

    })
})

    var server_port = process.env.YOUR_PORT || process.env.PORT || 5000;
    http.listen(server_port, () => {
        console.log("Started on : " + server_port);
})

這是完整的錯誤:

internal/modules/cjs/loader.js:311
  throw err;
  ^

Error: Cannot find module 'C:\Users\rami_\OneDrive\Skola\Utbildning\White board\server\node_modules\socket-io\index.js'. Please verify that the package.json has a valid "main" 
entry
at tryPackage (internal/modules/cjs/loader.js:303:19)
at Function.Module._findPath (internal/modules/cjs/loader.js:516:18)
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:867:27)
at Function.Module._load (internal/modules/cjs/loader.js:725:27)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (C:\Users\rami_\OneDrive\Skola\Utbildning\White board\server\server.js:3:10)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32) {
  code: 'MODULE_NOT_FOUND',
  path: 'C:\\Users\\rami_\\OneDrive\\Skola\\Utbildning\\White board\\server\\node_modules\\socket-io\\package.json',
  requestPath: 'socket-io'
}

我已經運行npm install socket-io並且在 node_modules 文件夾中我可以看到一個名為socket-io的文件夾。

You have the wrong package name, it is socket.io , change the require to require('socket.io') and re-install it with npm, you're trying to use a deprecated package .

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM