Getting throw new ERR_INVALID_ARG_TYPE(name, ‘Function’, value);

I am using and getting this error:

throw new ERR_INVALID_ARG_TYPE(name, 'Function', value);

TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type function. Received undefined

I am making a controller for the callbacks on the socket, this is how it looks:

const { onDisconnect } = require('./app/controllers/socket');


io.on('connection', (socket) => {
    socket.on('disconnect', onDisconnect(socket));

I get the error right on the “onDisconect” function. However if I declare the function like this, it works:

io.on('connection', (socket) => {
    socket.on('disconnect', () => {
        console.log(`${} disconnected`);

I have the “onDisconect” function on another script, which contains this:

const onDisconnect = (socket) => {
    console.log(`${} disconnected`);

module.exports = { onDisconnect };


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

That’s because you’re calling onDisconnect(), which returns undefined, which is not a function. You have two options:

  1. Simply change socket.on("disconnect", onDisconnect(socket)) to socket.on("disconnect", onDisconnect)
  2. Return a function in onDisconnect:
function onDisconnect(socket) {
  return () => {
    console.log(`${} disconnected`);

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x