最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to import socket.io npm packege - Nodejs - Stack Overflow

programmeradmin7浏览0评论
const socketio = new Server();

import Server from 'socket.io';
SyntaxError: The requested module 'socket.io' does not provide an export named 'default'

const socketio = new Server();

import Server from 'socket.io';
SyntaxError: The requested module 'socket.io' does not provide an export named 'default'

Share Improve this question edited Dec 9, 2022 at 8:10 Zach Jensz 4,0786 gold badges17 silver badges31 bronze badges asked Dec 6, 2020 at 5:09 John JohnJohn John 1,4654 gold badges21 silver badges44 bronze badges 4
  • it should be import * as io from 'sockt.io' – BhaskerYadav Commented Dec 6, 2020 at 5:23
  • const app = express(); const server = createServer(app); const socketio = io(server); I am still getting the following error const socketio = io(server); ^ TypeError: io is not a function – John John Commented Dec 6, 2020 at 5:29
  • Does this answer your question? Pass options to ES6 module imports – turivishal Commented Dec 6, 2020 at 6:04
  • unfortunatly no – John John Commented Dec 6, 2020 at 6:06
Add a comment  | 

5 Answers 5

Reset to default 14

There are two kinds of exports: named exports (several per module) and default exports (one per module). It is possible to use both at the same time, but usually it is best to keep them separate.

Why are you receiving this error: The import statement you wrote, provides the Server which is not a default export. If socket.io had actually exported Server as below, then you would not get an error.

module.exports = {
  //Other exports
  Server as default
}

You could have done this:

import * as io from "socket.io"
import express from 'express';
import { createServer } from 'http';

const app = express(); 
const server = createServer(app); 
const socketio = new io.Server(server);

Edit:

You can import socket.io like this:

import { Server } from 'socket.io';
import express from 'express';
import { createServer } from 'http';

const app = express(); 
const server = createServer(app); 
const socketio = new Server(server);

I can not beleive Socket.io is making it so difficult to import their npm package.

Here is the answer. Thank you @MeghAgarwal

import { Server } from 'socket.io';
import express from 'express';
import { createServer } from 'http';

const app = express(); 
const server = createServer(app); 
const socketio = new Server(server);

As of v3, the correct way to do it is:


const httpServer = require('http').createServer((req, res) => {
  res.setHeader('Content-Type', 'text/html');
  res.setHeader('Content-Length', Buffer.byteLength(content));
  res.end(content);
});
// Or const httpServer = require('http').createServer(app) if you use express

const io = require('socket.io')(httpServer);

And if you import * as io from 'socket.io', you got to call io.io(httpServer). I ran into the same error

I got the same error. This works for me

const express = require('express');
const app = express();

const io = require("socket.io")(80);
const http = require("http");
const server = http.createServer(app);

I just made a silly mistake ...instead of installing socket.io I installed outdated socketio which doesn't have support for TS.

发布评论

评论列表(0)

  1. 暂无评论