Getting started with Haxe for Node.JS

1

Install Haxe

1. Download and install the Haxe Compiler.
2. Run this command in your console to globally install Node.JS support in Haxe:
haxelib install hxnodejs
2

Install a Haxe-compatible IDE

It is recommended to install one of these to develop your Haxe projects.
3

Create your project

1. Create a new directory for your Haxe project, with this structure:
project/
├── src/
│   └── Main.hx
│
├── bin/
│   └── js/
│
└── build.hxml
2. Insert the following code into the src/Main.hx file:
class Main {
  static function main() {
    Sys.println("Haxe is great!");
  }
}
3. Add the following build configuration into the build.hxml file:
-lib hxnodejs
-cp src
-main Main
-js bin/js/main.js
The configuration above specifies that your source code is stored in src/, that your main Haxe file is src/Main.hx, and that you want Haxe to output the JavaScript source code into bin/js/main.js.
4

Install the Node.JS runtime

1. Install Node.JS.
2. Test your installation by opening a command prompt and typing node --version.
  • If your console cannot find node, you have an issue with your installation or node is not added into the PATH environment variable.
  • If the installation was successful, you should see something like the following:
v10.16.0
5

Develop your project

Haxe API

Browse the Haxe API website for the core Haxe APIs that you can use in your project.
You can use JavaScript-specific APIs in addition to the core Haxe APIs, as well as Node.JS-specific APIs.

Haxe libraries

Browse the haxelib website for community-developed libraries that you can add to your project.
You can install haxelib libraries globally by running haxelib install <library>.
You can specify that your project uses a haxelib by adding -lib <library> to the build.hxml file.

6

Compile and run your project

Compilation

Build the Haxe project by running haxe build.hxml in a console in the project directory.
This compiles your .hx source code into JavaScript code which you can run and debug next.

Running and debugging

After compiling your project with Haxe, you'll need to open a console in the bin/js directory and run the command node main.js. If you haven't changed the example from step 3, you should see the 'Haxe is great!' log message. Node.JS provides a built-in debugger. To output a debugger statement, use js.Lib.debug().

7

Ask the community

If you need help with anything, visit the vibrant Haxe community and simply ask for help with the details of your project. We have a helpful and active community and you should get your answers quickly!