Compilation failure when using -npm flag with html5

When building a HaxeFlixel project with lime and passing the npm flag, I get a compilation failure. Here’s the command and result:

lime test html5 -debug -npm
...
ERROR in ../haxe/debug.hxml
Module Error (from ./node_modules/haxe-loader/index.js):
--macro:1: character 0 : Class<IntIterator> should be Null<String>

ERROR in ../haxe/debug.hxml
Module Error (from ./node_modules/haxe-loader/index.js):
--macro:1: character 0 : For optional function argument 'path'

ERROR in ../haxe/debug.hxml
Module Error (from ./node_modules/haxe-loader/index.js):
--macro:1: character 0 : { } should be Null<String>

ERROR in ../haxe/debug.hxml
Module Error (from ./node_modules/haxe-loader/index.js):
--macro:1: character 0 : { } should be String

ERROR in ../haxe/debug.hxml
Module build failed (from ./node_modules/haxe-loader/index.js):
Haxe Loader: Compilation failed
haxe -D webpack_namespace=debug -main ApplicationMain -cp "/Documents/Haxe/flixel/5,8,0" -D "flixel=5.8.0" --macro "lime._internal.macros.DefineMacro.run()" -cp "/Documents/Haxe/lime/8,1,2/src" -D "lime=8.1.2" --macro "keep(IntIterator)" -cp "/Documents/Haxe/hscript/2,4,0" -D "hscript=2.4.0" -cp "/Documents/Haxe/flixel-addons/3,2,1" -D "flixel-addons=3.2.1" -cp "/Documents/Haxe/crypto/1,0,4/src" -D "crypto=1.0.4" --macro "openfl.utils._internal.ExtraParamsMacro.include()" -cp "/Documents/Haxe/openfl/9,3,2/src" -D "openfl=9.3.2" -cp "/Documents/Haxe/test/source" -D "queue_experimental_optimization" -D "howlerjs" -D "EMSCRIPTEN_SDK=/Documents/GitHub/emsdk/upstream/emscripten" -D "lime-webgl" -D "lime-dom" -D "lime-howlerjs" -D "lime-html5" -D "openfl-disable-handle-error" -D "FLX_NO_FOCUS_LOST_SCREEN" -D "tools=8.1.2" -D "lime-canvas" -D "no-compilation" -D "openfl-html5" -D "SHADERS_SUPPORTED" -D "FLX_NO_SOUND_TRAY" -D "html5" -D "web" --remap flash:openfl --macro "flixel.system.macros.FlxDefines.run()" -js /tmp/tmp-69574nXNVBNBzoyx.js -cp "/Documents/Haxe/test/export/html5/haxe" -D "html5" -debug
ℹ 「wdm」: Failed to compile.

Building without the npm flag works without issue. When running the command without the debug flag, the issues instead occur in release.hxml.

After a little investigation, it appears that when you use the -npm flag, it currently always compiles your code with Haxe 3.4.7. So the compiler errors that you are getting are likely because some code in your project is expecting Haxe 4, and Haxe 3.4.7 simply doesn’t understand it. This is an unfortunate downside of using the -npm flag.

When you use the -npm flag, it generates a package.json file, which includes a dependency on the haxe npm module. The package.json template has a haxeDependencies section which hard-codes Haxe 3.4.7, and that module will download a completely separate Haxe compiler than the one that is normally used when you don’t specify the -npm flag.

You can make it use a newer Haxe compiler if you provide a custom template for package.json using the <template/> tag in your project.xml.

I have several samples for using custom templates, but not one specifically for the package.json file when using the -npm flag. However, here’s one that replaces index.html, which is similar enough:

It should be very similar for a custom package.json, but where exactly you place your custom package.json is slightly different. While index.html should be at custom-templates/html5/template/index.html, package.json should be here instead: custom-templates/html5/npm/package.json. The path is figured out by looking where the files exist inside Lime. Here’s a link to the html5 template files in the Lime repository:

And here’s a link to the package.json file with the haxe version line highlighted:

You’d just change that line to specify a newer version in your copy of the template file. It will still download a separate Haxe compiler, but you can at least ensure that it matches the version of Haxe that you prefer. Unfortunately, there’s no way to tell npm to use the system Haxe compiler instead.

1 Like

Thanks for the resources! I confirmed that the template files are loaded by using a custom index.html and I no longer see the --macro errors, but I have the one generic error:

ERROR in ../haxe/debug.hxml
Module build failed (from ./node_modules/haxe-loader/index.js):
Haxe Loader: Compilation failed

I tried -verbose when compiling, but that’s still all it says. Do I have to add more info to the package.json file or are there other things I can test?