Friday, 27 September 2013

Client on node: Uncaught ReferenceError: require is not defined

Client on node: Uncaught ReferenceError: require is not defined

So, I am writing down an application with the node/express + jade combo.
I have a file called say, client.js, which is loaded on the client. In
that file I have code that calls functions from other javascript files. My
attempt was to use a statement of the form
var m = require('./messages');
in order to load the contents of the file messages.js (just like I do on
the server side) and later on call functions from that file. However,
clearly require is not defined on the client side, and it throws an error
of the form "Uncaught ReferenceError: require is not defined".
These other javascript files are also loaded on runtime at the client
because I place the links at the header of the webpage, so, clearly, the
client knows all the functions that are exported from these other files.
Hence, my question is:
How do I call these functions from these other javascript files (such as
messages.js) in the main client.js file that opens the socket to the
server and does all the job?

No comments:

Post a Comment