samedi 9 mai 2015

Load module using node.js require

I'm pretty new to node.js and I've been stuck for quite a long time on a problem I can't solve. I'm building a node.js app using express and jade frameworks. The app has the standard skeleton, that's how looks the main directory :

  • app.js
  • bin
  • npm-debug.log
  • views
  • node_modules
  • package.json
  • public

The problem occurs when I try to load the socket.io module in a js file (called lets say x.js) within the public directory. That's where I've implemented some logic tightly connected to user-action event handling. So in other words when someone clicks on a button 'connect', I'd like to establish connection using socket.io. The problem is that when I add this line

var socket_io = require('socket.io'); in x.js

the whole functionality suddenly stops working, I guess due to the fact that the module is not loaded although the var socket_io isn't used anywhere below within the x.js file. If I add the line var socket_io = require('socket.io'); to app.js everything works. I looked into this SO question but with no success. Can someone explain why is this happening and what am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire