ilearnio / module-alias

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

Path Resolution Issue on Windows #42

Closed dtromblee closed 6 years ago

dtromblee commented 6 years ago

When attempting to use this module on Windows, the base path resolution specified in package.json works, but final file path resolution doesn't seem to be processed by path.join() (e.g. returned path to require is something like 'C:\Users\aUser\appsub-folder/file-name, instead of 'C:\Users\aUser\app\sub-folder\file-name).

Did some light debugging and it seems like the Module._resolveFilename function does remap the file's path correctly in one of its iterations, but then it's attempting to call the erroneous request path mentioned above, and node errors out. This might be a quirk outside the scope of this module, but I thought I'd start here and work my way up.

For reference, my package.json config and require calls in code are below. Please let me know if there's anything I've omitted or misconfigured, though I tried to follow the README to my best ability. Also tried different file variations (without trailing /, with leading ./).

package.json

{
  "_moduleAliases": {
    "@controllers": "controllers/",
    "@models": "models/"
  }
}

app.js

require('module-alias/register');
const UserService = require('@controllers/user-service');
const AuthService = require('@controllers/auth-service');
ilearnio commented 6 years ago

but then it's attempting to call the erroneous request path mentioned above, and node errors out

Can you please provide the error details?

That is strange that you have this error, never heard about it happening to anyone who's on Windows.

dtromblee commented 6 years ago

Sure, see below. I don't believe I have any other modules installed that should interfere, but if anything looks off let me know, and I can tweak.

internal/modules/cjs/loader.js:582
    throw err;
    ^

Error: Cannot find module 'C:\Users\dtrom\projects\node-react-appcontrollers/user-service'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:580:15)
    at Function.Module._resolveFilename (C:\Users\dtrom\projects\node-react-app\node_modules\module-alias\index.js:49:29)
    at Function.Module._load (internal/modules/cjs/loader.js:506:25)
    at Module.require (internal/modules/cjs/loader.js:636:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (C:\Users\dtrom\projects\node-react-app\controllers\auth-service.js:2:21)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
ilearnio commented 6 years ago

What if you will do this, will it work?

require('C:\Users\dtrom\projects\node-react-appcontrollers\user-service')

If it's a folder maybe index.js is missing?

dtromblee commented 6 years ago

Ok, so I did a bunch of more trial and error tests, and I think this had to do either Window's odd file buffer handling (happened to me before with a different issue), or the fact that I reorganized the file structure recently and hadn't closed down my editor/git bash.

Working now though, thanks for the help!