I have a basic Node.js+socket.io app, and want to include coffee-script file. It was easy to include the file with
require('coffee-script');
items = require('./brain/items.coffee');
and it works - console.log throw debug info. But I couldn't access variables, functions and other stuff from the file. Also in the compiled version of the imported file it looks so:
(function() {
var a,run;
a = 'ok';
run = function() {
return console.log('all-oik!');
};
}).call(this);
so it looks for me like it run in its own scope and really not accessible from the other script (am I wrong? I'm not so good with advanced JS).
How could I work around this and take cofffee content from the main app?
PS: main app is a plain .js file, not coffee-script, if it matters.
This is no coffee-script specific problem. Have you tried importing functions from a normal js file in your main file? How did that go?
You can read one of many handy intros: http://openmymind.net/2012/2/3/Node-Require-and-Exports/
What you'll discover is that node's require uses commonjs modules, and to make something available outside a module, you need to assign it to the module.exports
variable.
so in your case: (in coffee-script)
module.exports.a = 'ok'
module.exports.run = () ->
return console.log 'all-oik!'