ilearnio / module-alias

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

Cannot find module when works with npm scoped packages #91

Open yijiasu opened 4 years ago

yijiasu commented 4 years ago

I am getting an issue when using module-alias with some private npm scoped packages.

For example, given we have defined _moduleAliases configuration in package.json, it works well without any private scoped packages (which also has a name starts with @ sign).

If I import any scoped packages (in my case, the name is @org/utility), we will run into a issue like this:

Error: Cannot find module 'org/utility'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:582:15)
    at Function.Module._resolveFilename (/Users/***/node_modules/module-alias/index.js:49:29)
    at Function.Module._load (internal/modules/cjs/loader.js:508:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous> (/Users/***/dist/utils/ccd.js:14:25)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)

It looks like module-alias try to resolve this package as an alias definition while actually it's not. If I manually require package without module-alias hooked, everything works well again.

Kehrlann commented 4 years ago

Hey @yijiasu , thanks for reporting !

Not sure exactly how private scoped packages work, but I'm not sure the problem is that module-alias is trying to load @org/utility as an alias. Unless you have @org as an alias as well, which might conflict. Using @ for aliasing is just an example, you could use $$$ instead, it would make no difference to module-alias.

Would you mind sharing your package.json ? Even better would be a git repo with everything needed to reproduce the problem. Also, which version of node are you using ?