Open gbkwiatt opened 4 years ago
Hi @gbkwiatt, thanks for reporting !
I believe this is a know issue, see #59 . Imports are slightly different in esm; so the hack module-alias
uses won't work.
I kind of made it work. But do I understand why ? no ...
There is something I don't understand about ES6 - esm.
I was requiring my express app in server with "require" and then it works,
but if I use import, non of my require works, even inside my project
It kind of looks like, require('module-alias/register')
is executed too late
I've change everything to use import instead of request, but unfortunately it doesn't work at all. what a shame.
If I recall correctly, when you use require()
you use one way of importing modules, which uses the node module called ... module
. In there, module-alias overrides a private function, _resolveFileName
, to look for our aliases on top of the normal thing it does.
However, when you use import
, node does not use module._resolveFileName
, and so it doesn't see your aliases.
Workaround via https://stackoverflow.com/questions/53200528/esm-does-not-resolve-module-alias
nodemon -r esm -r module-alias/register src/index.js
@malammar really cool, thank you ! Do you want to submit a PR to update the README with your solution ?
Is there a way to use -r module-alias/register
while defining the aliases from JS instead of package.json
?
If I recall correctly, when you use
require()
you use one way of importing modules, which uses the node module called ...module
. In there, module-alias overrides a private function,_resolveFileName
, to look for our aliases on top of the normal thing it does.However, when you use
import
, node does not usemodule._resolveFileName
, and so it doesn't see your aliases.
So, how do I use module along with module-alias? It works perfectly when I don't use type: module
.
You can use this module instead
I found a way to make nodemon work, typescript with module_alias.
Your package.json should look like this:
"_moduleAliases": {
"@": "src",
"@": "dist"
}
After that, every update in the TS code will be recompiled by the nodemon using the custom paths.
I just wanted to use it with my nodejs 14x app.
I run my node with esm module for ES6 support.
and packages.json
And I keep getting error
Cannot find module '_root/logger'
Tried with different combinations of directories and names always same result. Is it because I run my app withnode -r esm -r dotenv/config server.js
esm ?