Having trouble getting Haxe NodeJS off the ground

(Kyliathy) #1

I’m trying to set up a NodeJS backend for use with my Haxe client JS application. Unfortunately, I can’t find any working example. The official haxe nodejs library has seen quite little action lately. I’ve tried using an example from here:

But it doesn’t compile with my IntelliJ IDEA Haxe plugin. The following imports cannot be found:

import js.node.Http; import js.Node.console;

This also seems to be out of date:

Could there be a problem with my IntelliJ configuration? I set libraries and source paths in Project Structure and I think they’re ok?

(Eric Bishton) #3

The first directory for hxnodejs is right. It should be pointing to the version path. The Haxe plugin will look at the haxelib.json to find the proper source roots.

However, the js-kit entries should be in another library – because only one haxelib.json is expected for each library, so none of those sources will be found.

You also have to add the library dependencies to the module you are building.

(Kyliathy) #4

@ebishton done & done :). Still acting up. I’m getting code completion for js.Node.http. It capitalizes Node but when autocompleting “Http” to finalize the import it makes it to “node”. It can’t find either. When I investigate “External Libraries” via IntelliJ Project Explorer I can see these packages. But neither code completion NOR compilation works.

Error:(3, 7) Type not found : js.node.Http

Here’s how my project structure looks now:

and must post again for the next image.

(Kyliathy) #5

(Kyliathy) #6

Anybody? :frowning:

(Rudy Ges) #7

Sorry, never used intellij.

(david mouton) #8

It works for me.

I used haxe 3.4.7 and hxnodejs from NPM.
I fixed your class like that :

package ;

import js.Node;
import js.node.Http;

    Example web server from the node.js main page
class WebServer {
    static function main() {
        Http.createServer(function (req, res) {
            res.writeHead(200, {'Content-Type': 'text/plain'});
            res.end('Hello World\n');
        }).listen(1337, '');

        Node.console.log('Server running at');

This his how i configure Intellij :

You can also have a look on this nodejs haxe project, coded with Intellij:

(Kyliathy) #9

@damoebius It worked :). Actually I think @ebishton’s answer is also good. It’s my bad that I didn’t actually point to the sources folder!

@damoebius Oh, you made a game with Haxe, nice! :). Did you find that hxnodejs is sufficiently up to date to use the latest features of Node? How do you work with Node when you recompile often? Use something like nodemon?

Thank you all :slight_smile:

(david mouton) #10

Yes it’s. There is a lot of externs and if one is missing it’s easy do it.

And yes nodemon is usefull :slight_smile:

(Kyliathy) #11

@damoebius Thank you! :). One more question, on topic: how do you debug? :slight_smile:

From what I understand you linked some of your node externs? But they’re not specifically for node itself, but for libraries that you use with node, or am I missing something?

(david mouton) #12

I debug only my generated javascript from Chrome thanks to nodemon.

(Kyliathy) #13

@damoebius But how do you inspect what happens on the server. Breakpoints, watch variables, stack traces, etc?

(david mouton) #14

All of them :slight_smile:
I run my server locally with “nodemon --inspect build\server.js”, so the chrome debuger can connect to my nodejs instance.

With Intellij Ultimate or WebStorm you also have a good nodejs integration.

(Kyliathy) #15

@damoebius Things are starting to work, thanks to you :). I’m a bit of a noob when it comes to Node however. I see you use express. I’m looking at your code trying to determine how I can make Node respond differently to a different URL request, but I’m stuck at the step of adding Express to my project.

How did you add this to your project? I assume I install express using NPM and use your Haxe externs class, but I assume I have to add express sources somehow to my project.

(david mouton) #16

Mmm i don’t know why express is missing in the latest version of my package.json

Anyway, you need to add both express et hxexpress to your devdependencies

"devDependencies": {
    "body-parser": "~1.0.1",
    "express": "~4.0.0",
    "hxexpress": "^1.0.0"

and then “npm install”

(Kyliathy) #17

A-ha! It was your “hxexpress” mention I was needing :slight_smile: . I don’t use NPM to build my project. I just use IntelliJ and haxelib to get required packages. That seems to be enough.

In the meantime I noticed that Chrome developer tools keeps getting disconnected every time nodemon restarts the server. Then I have to copy paste the new URL into Chrome which is somewhat of a chore :slight_smile: .

Also, I don’t see my HX files :frowning: . I only see the compiled JS. Chrome says it detects the source map, but unfortunately nothing is there, unlike my client application which works fine (Chrome shows HX files and I can debug them).

Thank you for dedicating a bit of your time for all this. Haxe is a small community compared to others and not that many people to help out. I think I’m going to be more active around here too :).

I’m barely starting with a story-based game (interactive story). Haxe is really refreshing as a language :slight_smile: . I feel like on an adventure, which goes perfectly hand in hand with the open world interactive story I’m building :smiley:

(david mouton) #18

Arg, i can’t help you with source map usage at nodejs side. Javascript output is enough readable for me :confused:

Maybe we can ask @elsassph, or @back2dos ?

(Kyliathy) #19

@damoebius I’m cloning your game now, looking into learning some stuff from there. It’s an interesting project I think. I like the concept of training AIs :). I’m actually also into some AI stuff for a possible project. Would you be interested in connecting outside this Forum a bit? If so, can you share a contact method?

(Philippe) #20

@damoebius @Kyliathy I don’t have any problem running with sourcemaps using either:

  • Chrome remote debugger:
    • node --inspect-brk output.js
    • Open chrome://inspect in Chrome and find your JS
    • in Sources tab, Ctrl+O or Ctrl+P and type a Haxe file name
  • VS Code debugger:
    • create a launch configuration as below, add breakpoints, and run (F5)
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "program": "${workspaceFolder}/output.js"

PS: in both cases compile with -debug to generate sourcemaps.