@:jsRequire <> js.Lib.require inconsistency?

I’m trying to get the default object from a js module when requiring it using js.Lib.require(), but Haxe complains that default is unexpected, here:

var foo = js.Lib.require('the-module').default

Why can’t I use default in that context? It works with other identifiers.

Either way, it seems there’s some inconsistency between @:jsRequire and js.Lib.require, shouldn’t the latter (also) accept an optional second argument to specify the object to load, if any?

Regarding the first question: simply because default is a keyword. I guess you’d have to use Reflect.field() as a workaround, or define an extern class with @:native() meta.

Yes, I defined an extern with @:jsRequire('module', 'default'), but I’d have preferred to just have it inline in this case. How would @:native help in this scenario?

You could probably do something like this:

extern class Module {
    @:native('default') public var default_:SomeType;
}
var foo = (js.Lib.require('the-module') : Module).default_;
1 Like

Thanks @Gama11 :slight_smile:

I’ll also add here what @back2dos suggested on gitter for future reference and if anyone else stumbles upon this same issue:

@fullofcaffeine js.Lib.require(‘the-module’)[cast “default”] will do
or just untyped require(‘the-module’)[‘default’]

Thanks @back2dos