inxilpro / node-app-root-path

Determine the root path to your project
MIT License
605 stars 29 forks source link

Wrong app root path on Windows with scoped packages and nvm #20

Closed goloroden closed 1 year ago

goloroden commented 7 years ago

I have globally installed a Node.js application on Windows, where Node.js was installed using nvm-windows. This works.

What's unusual about my scenario is that the module is a scoped one, i.e. there is an @org prefix in its name, which results in an additional directory. So the path looks like this:

C:\Users\jane.doe\AppData\Roaming\nvm\v6.7.0\node_modules\@org\my-app

This is the application's root path. Now inside of this application I am using a module. It gets installed to the application's node_modules directory, so we end up with:

C:\Users\jane.doe\AppData\Roaming\nvm\v6.7.0\node_modules\@org\my-app\node_modules/my-module

Now, this module uses app-root-path to get the root path of the application.

The expected result is:

C:\Users\jane.doe\AppData\Roaming\nvm\v6.7.0\node_modules\@org\my-app

The actual result is:

C:\Users\jane.doe\AppData\Roaming\nvm\v6.7.0

Obviously app-root-path detects the root path in a wrong way, but I am not sure why. It may be that it is because of the additional node_modules directory (which, I guess, is caused by nvm on Windows), or it may be because the application is a scoped packages.

Unfortunately, as I am not really sure about the cause, I can not provide a fix. Any ideas on this?

inxilpro commented 7 years ago

Can you post what require('module').globalPaths contains in your app?

goloroden commented 7 years ago

I'll hand it over to a colleague whom I asked this for.

FrankStraif commented 7 years ago

Hi,

require('module').globalPath contains:

[ 'C:\\Users\\jane.doe\\.node_modules', 'C:\\Users\\jane.doe\\.node_libraries', 'C:\\Program Files\\lib\\node' ]

Regards, Frank

inxilpro commented 7 years ago

Thanks, Frank. I'm not entirely sure how nvm handle setting its module path on Windows. It's odd that C:\Users\jane.doe\AppData\Roaming\nvm\v6.7.0\node_modules isn't in that list. What about your NODE_PATH environmental variable? What is that set to?

FrankStraif commented 7 years ago

Hi Chris, I have not set NODE_PATH (never needed it). But even setting NODE_PATH to e.g. "C:\Users\jane.doe\AppData\Roaming\nvm\v6.7.0" did not change the behavior, expect that require('module').globalPath now returns

[ '"C:\Users\jane.doe\AppData\Roaming\nvm\v6.7.0"', 'C:\Users\jane.doe\.node_modules', 'C:\Users\jane.doe\.node_libraries', 'C:\Program Files\lib\node' ]

appRootPath.path is still set to "C:\Users\jane.doe\AppData\Roaming\nvm\v6.7.0".


Remember: the module is globally installed: I now debugged a little bit and in resolve.js line 49 the following happens: 1) resolved is set to:

c:\Users\jane.doe\AppData\Roaming\nvm\v6.7.0\node_modules\@org\my-app\node_modules\app-root-path

2) the code does:

var nodeModulesDir = sep + 'node_modules';

if (!alternateMethod && -1 !== resolved.indexOf(nodeModulesDir)) {
    var parts = resolved.split(nodeModulesDir);
    if (parts.length) {
        appRootPath = parts[0];
        parts = null;
    }
}

3) This is how the parts then look like:

parts:
0:"c:\Users\jane.doe\AppData\Roaming\nvm\v6.7.0"
1:"\@org\my-app"
2:"\app-root-path"

4) and parts[0] is what is finally returned as appRootPath.path.

Hope this helps.

Thanks, Frank

navossoc commented 7 years ago

I have the exactly the same problem (equivalent paths also), but I'm not using nvm or scoped packages.

I'm not sure, but maybe this isn't related to the new "npm flat design packages"? I'm not a "node expert", so I'm just trying to guess here...

goloroden commented 6 years ago

Any update on this?

beanilsson commented 2 years ago

Still experiencing this. Any updates?