'"TypeError: require(...) is not a function" in nodejs and socketio

I am creating a chat with NodeJS, Express and Socket.Io, but I have this error when I want to run the code:

require('./sockets')(socketio);
                    ^

TypeError: require(...) is not a function
    at Object.<anonymous> (C:\Users\miri pc\Documents\chatjavascript\src\index.js:11:21)
    at Module._compile (node:internal/modules/cjs/loader:1099:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

there is the code:

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

const app  = express();

const server = require('http').Server(app);
const socketio = require('socket.io')(server);

app.set('port', process.env.PORT || 3000);

require('./sockets')(socketio);

//archivos estáticos
app.use(express.static(path.join(__dirname, 'public')));

server.listen(app.get('port'), () => {
    console.log('conectado en el puerto', app.get('port'));
})

My Socket.Io version is 4.5.0, NodeJS is v17.9.0 and Express is 4.18.1



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source