Consider this
//Haxenode.hx class Haxenode { @:expose("hello") public static function hello(){ return "hello"; } }
@:expose("hello") part is to put something in module.exports .
Now run
haxe -js haxenode.js -dce no Haxenode
Now you can use haxenode.js in nodejs
var haxenode = require('./haxenode.js'); var hello = haxenode.hello;
So this taken together is the answer to your question:
var cp = require('child_process'); function requireHaxe(haxeClassPath,cb){ //generate a JavaScript module from the Haxe file, and then return the generated JavaScript module cp.exec('haxe -js haxenode.js -dce no ' + haxeClassPath,function(err){ if (err){ cb(err); return; } cb(null,require('./haxenode.js')); }); }
Note that the output file name is a stub.
But do not do this - it is better to compile haxe as a build step (with all the necessary compilation options), and then use regular require at run time.
source share