itaylor / redux-socket.io

An opinionated connector between socket.io and redux
410 stars 53 forks source link

Getting error - `createSocketIoMiddleware is not a function` when using node's require() #24

Closed robguy21 closed 7 years ago

robguy21 commented 7 years ago

Hey man!

First off, thanks for the awesome work.

Onto my issue- I have a use case where I need to use redux-socket.io on the server side, but I'm immediately hitting a brick wall.

Here's my store initialization on the server...

//** Redux Stuff **//
const createStore = require('redux').createStore;
const thunk = require('redux-thunk');
const reducers = require('../server-reducers');

const secrets = require('../secrets.json');

const createSocketIoMiddleware = require('redux-socket.io');

// We need the socket object as a parameter here because socket connects...
// before the store is initialized.
module.exports = function createServerStore(io) {

  const socketIoMiddleware = createSocketIoMiddleware( //** Boop! '... is not a function'
    io,
    ['server/', 'HiBoards::', 'HiSocket::']
  );

  io.emit('Initialised Store for Connection', { data: 'yo' });
  console.log('Store Initialized');

  return createStore(
    reducers,
    compose(
      applyMiddleware(
        thunk,
        socketIoMiddleware
      )
    )
  );
}

Not sure why this is happening on the backend but the front-end is working fine, might this be an issue with require() vs import?

Versions below

nodejs: 6.9.2
npm: 3.10.8
createSocketIoMiddleware: 1.3.1
itaylor commented 7 years ago

If you're not using babel to transpile import to require in your server side node code, you'll need to require('redux-socket.io').default;

This is just the way that default exports work when required from commonJS. http://stackoverflow.com/questions/33704714/cant-require-default-export-value-in-babel-6-x/33705077