module-alias icon indicating copy to clipboard operation
module-alias copied to clipboard

Doesn't work with esm ?

Open gbkwiatt opened this issue 5 years ago • 9 comments

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 ?

gbkwiatt avatar May 25 '20 19:05 gbkwiatt

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.

Kehrlann avatar May 25 '20 19:05 Kehrlann

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.

gbkwiatt avatar May 25 '20 19:05 gbkwiatt

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.

Kehrlann avatar May 25 '20 20:05 Kehrlann

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

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

malammar avatar Aug 08 '20 23:08 malammar

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

Kehrlann avatar Aug 10 '20 07:08 Kehrlann

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

PierBover avatar Jan 27 '22 17:01 PierBover

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.

ali-ahnaf avatar Apr 04 '22 16:04 ali-ahnaf

You can use this module instead

euberdeveloper avatar Jul 18 '22 23:07 euberdeveloper

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.

gcboaventura avatar Nov 18 '22 20:11 gcboaventura