How to import socket.io npm packege - Nodejs

Joh*_*ohn 4 javascript node.js socket.io

Any idea why I am getting an error?

  import Server from 'socket.io';
  const socketio = new Server();
Run Code Online (Sandbox Code Playgroud)

Error

import Server from 'socket.io';
       ^^^^^^
SyntaxError: The requested module 'socket.io' does not provide an export named 'default'
Run Code Online (Sandbox Code Playgroud)

Meg*_*wal 7

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
}
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)