#1934·yargs

[v17, ESM] version command not working when project installed globally

Author: wKovacs64Created May 3, 2021Updated Aug 2, 2026
Labelsbug

[kGuessVersion]() isn't working (returning unknown) as escalade can't find the correct package.json because it's being passed an incorrect value for startDir. The issue seems to be with mainFilename in the ESM platform shim. I believe this only impacts ESM projects that are installed globally (?).

See the following: https://github.com/yargs/yargs/blob/a1fe8639365b8c0f3f0f099a9d7140ee9e48d25c/lib/yargs-factory.ts#L1550 https://github.com/yargs/yargs/blob/a1fe8639365b8c0f3f0f099a9d7140ee9e48d25c/lib/platform-shims/esm.mjs#L36

If I add debug logging to the ESM shim, I get the following (installed globally with Yarn v1 on Windows, but I get the equivalent results using npm on macOS):

{
  mainFilename: 'C:\\Users\\User\\AppData\\Local\\Yarn\\Data\\global\\',
  __dirname: 'C:\\Users\\User\\AppData\\Local\\Yarn\\Data\\global\\node_modules\\project\\node_modules\\yargs\\lib\\platform-shims\\esm.mjs',
  'process.cwd()': 'C:\\Users\\User'
}

(none are correct, as far as I can tell)

The first item resulting from splitting on node_modules doesn't seem to be sufficient?

https://github.com/yargs/yargs/blob/a1fe8639365b8c0f3f0f099a9d7140ee9e48d25c/lib/platform-shims/esm.mjs#L18

Ran out of debugging time but wanted to get what I had out here. Let me know if there's more I can do to help.


Thank you for all your work!