ilearnio / module-alias

Register aliases of directories and custom module paths in Node
MIT License
1.76k stars 69 forks source link

Doesn't work with esm ? #99

Open gbkwiatt opened 4 years ago

gbkwiatt commented 4 years ago

I just wanted to use it with my nodejs 14x app.
I run my node with esm module for ES6 support.

require('module-alias/register')
import logger from '_root/logger'

and packages.json

  "_moduleAliases": {
    "_root": "."
   }

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 with node -r esm -r dotenv/config server.js esm ?

Kehrlann commented 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.

gbkwiatt commented 4 years ago

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.

Kehrlann commented 4 years ago

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.

malammar commented 4 years ago

Workaround via https://stackoverflow.com/questions/53200528/esm-does-not-resolve-module-alias

nodemon -r esm -r module-alias/register src/index.js

Kehrlann commented 4 years ago

@malammar really cool, thank you ! Do you want to submit a PR to update the README with your solution ?

PierBover commented 2 years ago

Is there a way to use -r module-alias/register while defining the aliases from JS instead of package.json?

Propo41 commented 2 years ago

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.

So, how do I use module along with module-alias? It works perfectly when I don't use type: module.

euberdeveloper commented 2 years ago

You can use this module instead

gcboaventura commented 2 years ago

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.