NodeJS Haxe Module

Hi,
I want to make my own nodejs module.
It should looks like :

var myAwesomeModule = {
  postmessage: function () {
    // whatever
  },
  onmessage: function () {
    // whatever
  }
}
module.exports = myAwesomeModule; 

I can’t do it in haxe.

Any idea ?

class MyAwesomeModule {
  static function main() {
    untyped module.exports = {
      postmessage: function () {
        // whatever
      },
      onmessage: function () {
        // whatever
      }            
    }
  }
}

And build with haxe -js my-awesome-module.js -main MyAwesomeModule. Note that this will of course entirely sabotage @:expose :smiley:

1 Like

In the same fashion, but in order to prevent the whole block expression to be untyped, you can also do this:

var module:Dynamic = js.Node.module;
module.exports = {
    postmessage: function () {
        // whatever
    },
    onmessage: function () {
        // whatever
    }            
};
2 Likes

If you don’t need to re-create the exports object you can just add @:expose('postmessage') on a static function.

2 Likes