Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't work with esm ? #99

Open
gbkwiatt opened this issue May 25, 2020 · 9 comments · May be fixed by #107
Open

Doesn't work with esm ? #99

gbkwiatt opened this issue May 25, 2020 · 9 comments · May be fixed by #107

Comments

@gbkwiatt
Copy link

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
Copy link
Collaborator

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
Copy link
Author

gbkwiatt commented May 25, 2020

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
Copy link
Collaborator

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
Copy link

malammar commented Aug 8, 2020

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

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

@Kehrlann
Copy link
Collaborator

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

Kehrlann added a commit that referenced this issue Sep 27, 2020
@Kehrlann Kehrlann linked a pull request Sep 27, 2020 that will close this issue
Kehrlann added a commit that referenced this issue Sep 27, 2020
@PierBover
Copy link

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

@Propo41
Copy link

Propo41 commented Apr 4, 2022

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
Copy link

You can use this module instead

@gcboaventura
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants