tower-archive / tower

UNMAINTAINED - Small components for building apps, manipulating data, and automating a distributed infrastructure.
http://tower.github.io
MIT License
1.79k stars 120 forks source link

sockjs.js:11 io = Tower.module('socketio').createServer(); ^ TypeError: Cannot call method 'createServer' of undefined #334

Closed jeremysavoy closed 12 years ago

jeremysavoy commented 12 years ago

I'm running Mac OS X 10.8.2. Using fresh install of "nvm" I installed node v0.8.11, then the following via npm (which were just the latest versions):

coffee-script v1.3.3 coffeecup v0.3.17 mocha v1.6.0 grunt v0.3.16 tower v0.4.2-14 ejs v0.8.3 phantomjs v1.6.1 mondodb v1.1.9

Then ran the following, per the tutorial at http://nodehed.com/?p=15 :

> tower new towerBot

> cd towerBot

> npm link tower

> npm install

> cake watch

Then from a new shell ...

> node server (or node server.js)

And I get the following stack trace:

/Users/XXX/Projects/nodejs/nvm/v0.8.11/lib/node_modules/tower/lib/tower-net/server/connection/sockjs.js:11 io = Tower.module('socketio').createServer(); ^ TypeError: Cannot call method 'createServer' of undefined at Function.Tower.NetConnectionSockjs.listen (/Users/XXX/Projects/nodejs/nvm/v0.8.11/lib/node_modules/tower/lib/tower-net/server/connection/sockjs.js:11:35) at Tower.Application.Application.reopen.initializeSockets (/Users/XXX/Projects/nodejs/nvm/v0.8.11/lib/node_modules/tower/lib/tower-application/server/application.js:223:46) at Tower.Application.Application.reopen.listen (/Users/XXX/Projects/nodejs/nvm/v0.8.11/lib/node_modules/tower/lib/tower-application/server/application.js:204:14) at Tower.Application.Application.reopen.run (/Users/XXX/Projects/nodejs/nvm/v0.8.11/lib/node_modules/tower/lib/tower-application/server/application.js:275:19) at CommandServer.run (/Users/XXX/Projects/nodejs/nvm/v0.8.11/lib/nodemodules/tower/lib/tower-command/server/server.js:37:41) at .extend.run (/Users/XXX/Projects/nodejs/nvm/v0.8.11/lib/node_modules/tower/lib/tower/server.js:123:44) at Object. (/Users/XXX/Projects/nodejs/tower/towerBot/server.js:3:18) at Module._compile (module.js:449:26) at Object.Module._extensions..js (module.js:467:10) at Module.load (module.js:356:32)

thehydroimpulse commented 12 years ago

If your not changing tower's source you don't need npm link tower. A simple:

npm install tower -g
npm install grunt -g
npm install coffee-script -g
tower new towerBot
cd towerBot
npm install
# Start mongodb... Different for every OS...
# Start Redis if you want / have.
cake watch  # leave it open. It will watch for all your file changes.
node server.js # Run this in another tab.

With the latest version of Tower all these steps will succeed. (You may get another error #335 -> Still getting worked out.)

To make sure you have the latest Tower version you can run Tower -v and it should equal 0.4.2-14

thehydroimpulse commented 12 years ago

Ok so the issue is tied to the module loading in tower. Line #23 is the starting of the module loading and more specifically on line #35 has the socket.io module.

Only if it can require socket.io is it going to include it. Try manually installing socket.io within your app directory:

npm install socket.io
lancejpollard commented 12 years ago

I wonder if this has anything to do with nvm vs npm? The reason this may be happening is because the tower source is trying to require('socket.io'), but the socket.io dependency is installed in the app, so it would be like this:

./node_modules/socket.io
./node_modules/tower

but I'm not sure if ./node_modules/tower can load a module defined above it (I've had issues with this in the past, but it works fine now for me). What I know will work is this:

./node_modules/tower
./node_modules/tower/node_modules/socket.io

I would just like to avoid that b/c we'd have to add socket.io as a hard dependency for Tower which it isn't.

edubkendo commented 12 years ago

NVM shouldn't cause problems with npm, it's node version manager, and just installs instances of node into a folder in the user's home directory so as to avoid having to use so much sudo, and to make it easier to manage installing and uninstalling multiple versions. Perhaps the user could try deleting his node_module's folder, then:

npm cache clean
npm install .

And then repeating

cake watch
node server

Perhaps without linking tower (so that a copy is installed in the app's node_modules folder) this will work properly.

thehydroimpulse commented 12 years ago

@viatropos You could add socket.io as an optional dependency in the package.json. You can find the documentation at https://npmjs.org/doc/json.html towards the middle of the page. Not sure if this would partially solve it...

lancejpollard commented 12 years ago

Yes @edubkendo @jeremysavoy that is the issue. If you install tower globally, then do npm link tower in your project, it's not going to work.

The reason is because when you do npm install tower -g to install tower globally, it only installs the few modules required for the tower new <app-name>, and a few other tower commands. It doesn't need express, socket.io, and a lot of other stuff because, essentially, the tower command just needs to generate an app. Then you install tower in your app, and it includes many more modules (express, socket.io, etc.) required to actually run the server and all that.

However, if you clone the tower repo, then npm link that globally, then you can npm link tower in your app. This is because when you clone the tower repo and run npm install, it's going to install everything required for testing tower. So when you link it globally, then to your app, you already have every possible module tower will need. But if you try to link the global version of tower you installed with npm install tower -g, it won't have all those extra modules installed.

Try this instead:

npm install tower -g
tower new app
cd app
npm install

If you're working off the tower repo though, you can npm link it:

git clone https://github.com/viatropos/tower.git
cd tower
npm install # will install **everything** for testing
make # compile coffee-script's and such, or `make watch` for persistent compilation
npm link # makes it global
# then in some other directory
tower new app
cd app
npm link tower # link to github repo version of tower
npm install
lancejpollard commented 12 years ago

This may be kind of confusing, so I might just get rid of that installation optimization and just install everything everywhere. We'll see.

edubkendo commented 12 years ago

It was my mistake, sorry, instructions in my tutorial. At the time I started writing it that (linking from the global install) was still working. I've updated those now.

lancejpollard commented 12 years ago

Oh cool no worries. Your tutorial has been super helpful so far. Cheers.