Akhilendra yadav Akhilendra yadav - 3 months ago 29
Node.js Question

TypeError: object is not a function In Node JS / socket.io

I am very new with MEAN stack. I am facing "TypeError: object is not a function" while starting my server > node index.js.

.../master/server/app.js:47
require('./config/socketio')(socketio);
^
TypeError: object is not a function
at Object.<anonymous> (/home/divine20/master/server/app.js:30:2)
at Module._compile (module.js:456:26)
at loader (/home/divine20/master/node_modules/babel-core/node_modules/babel-register/lib/node.js:146:5)
at Object.require.extensions.(anonymous function) [as .js] (/home/divine20/master/node_modules/babel-core/node_modules/babel-register/lib/node.js:156:7)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/home/divine20/master/server/index.js:12:28)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3


My Index.js file is

'use strict';

// Set default node environment to development
var env = process.env.NODE_ENV = process.env.NODE_ENV || 'development';

if (env === 'development' || env === 'test') {
// Register the Babel require hook
require('babel-core/register');
}

// Export the application
exports = module.exports = require('./app');


App.js File

/**
* Main application file
*/

'use strict';

import express from 'express';
import mongoose from 'mongoose';
mongoose.Promise = require('bluebird');
import config from './config/environment';
import http from 'http';

// Connect to MongoDB
mongoose.connect(config.mongo.uri, config.mongo.options);
mongoose.connection.on('error', function(err) {
console.error('MongoDB connection error: ' + err);
process.exit(-1);
});

// Populate databases with sample data
if (config.seedDB) { require('./config/seed'); }

// Setup server
var app = express();
var server = http.createServer(app);
var socketio = require('socket.io')(server, {
serveClient: config.env !== 'production',
path: '/socket.io-client'
});
require('./config/socketio')(socketio);
require('./config/express')(app);
require('./routes')(app);

// Start server
function startServer() {
app.angularFullstack = server.listen(config.port, config.ip, function() {
console.log('Express server listening on %d, in %s mode', config.port, app.get('env'));
});
}

setImmediate(startServer);

// Expose app
exports = module.exports = app;


// Socketio.js

/**
* Socket.io configuration
*/
'use strict';

import config from './environment';

// When the user disconnects.. perform this
function onDisconnect(socket) {
}

// When the user connects.. perform this
function onConnect(socket) {
// When the client emits 'info', this listens and executes
socket.on('info', data => {
socket.log(JSON.stringify(data, null, 2));
});

// Insert sockets below
require('../api/utility/utility.socket').register(socket);
require('../api/repayment/repayment.socket').register(socket);
require('../api/transaction/transaction.socket').register(socket);
require('../api/offer/offer.socket').register(socket);
require('../api/payment/payment.socket').register(socket);
require('../api/yelp/yelp.socket').register(socket);
require('../api/faq/faq.socket').register(socket);
require('../api/blog/blog.socket').register(socket);
require('../api/user/user.socket').register(socket);
//require('../api/listing/listing.socket').register(socket);
}

export default function(socketio) {
// socket.io (v1.x.x) is powered by debug.
// In order to see all the debug output, set DEBUG (in server/config/local.env.js) to including the desired scope.
//
// ex: DEBUG: "http*,socket.io:socket"

// We can authenticate socket.io users and access their token through socket.decoded_token
//
// 1. You will need to send the token in `client/components/socket/socket.service.js`
//
// 2. Require authentication here:
// socketio.use(require('socketio-jwt').authorize({
// secret: config.secrets.session,
// handshake: true
// }));

socketio.on('connection', function(socket) {
socket.setMaxListeners(20);
socket.address = socket.request.connection.remoteAddress +
':' + socket.request.connection.remotePort;

socket.connectedAt = new Date();

socket.log = function(...data) {
console.log(`SocketIO ${socket.nsp.name} [${socket.address}]`, ...data);
};

// Call onDisconnect.
socket.on('disconnect', () => {
onDisconnect(socket);
socket.log('DISCONNECTED');
});

// Call onConnect.
onConnect(socket);
socket.log('CONNECTED');
});
}


Thanks in advance.

Answer

You're using a default export. It's likely that whatever you're using to compile your code (e.g. Babel or TypeScript) emits a default export by adding a property onto module.exports named default.

Try to write write require('./config/socketio').default(socketio).