Hello, World! I've been importing node libraries ...
# javascript
b
Hello, World! I've been importing node libraries and using them successfully, but for this one (express) I'm trying to use and am hitting something I don't understand. I've added this dependency:
Copy code
implementation(npm("express", "4.17.3"))
And declared this external:
Copy code
@file:JsModule("express")
@file:JsNonModule

package express

external fun express(): dynamic
But calling
express()
in my code gives me:
Copy code
TypeError: express_0 is not a function
Any idea why it's named
express_0
in the generated js? 🤔
t
Copy code
@file:JsModule("express")
@file:JsNonModule

package express

@JsName("default")
external fun express(): dynamic
?
a
Try adding
@JsName("express")
to the external definition
b
Thanks for the tips! Just tried (both
"default"
and
"express"
) but am still getting the same error.
t
Copy code
@JsModule("express")
external fun express(): dynamic
?
b
@turansky
@JsModule and @JsNonModule can't appear on here since the file is already marked by either @JsModule or @JsNonModule
t
Remove file annotation 🙂
b
yes that works! 🎉 First, thanks! 🙏 Second, any idea why? Annotating the whole file worked with other libraries.
t
• Old JS module style in
express
• Missed
JsModule
configuration
b
Missed
JsModule
configuration
sorry, not sure what you mean by this one
t
Member annotation cover all cases in ScalaJS:
Copy code
// old style
@JsImport(JsImport.Namespace)
external val express(): Express

// default import
@JsImport(JsImport.Default)
external val express(): Express

// named
@JsImport("express")
external val express(): Express
old style - your case
b
I see. Thanks a lot! I admit I'm not 100% sure I understand
JsModule
vs
JsNonModule
(and the docs I found were a bit taciturn)
t
Screenshot 2022-04-21 at 21.12.22.png
👍 2
@JsImport
is more flexible, cover all cases and it’s synchronous with
@JsExport
Also such scheme allows to declare extensions in the same file