zkat / npx

execute npm package binaries (moved)
https://github.com/npm/npx
Other
2.63k stars 105 forks source link

control the order of binary fallback hierarchy #134

Closed tabrindle closed 6 years ago

tabrindle commented 7 years ago

Hey! Just want to start out by saying this project is amazing, and I frequently tell anyone who is willing to listen about it, and how it will solve all their problems. #panacea

That being said, I frequently recommend this project in the context of my envinfo project which aims to make debugging environment related problems a bit easier. It's already in React Native, and about to be in a bunch of other projects like CRA.

The standard output looks like this: npx envinfo

Environment:
  OS: macOS High Sierra 10.13
  Node: 8.9.1
  Yarn: 1.3.2
  npm: 5.5.1
  Watchman: 4.9.0
  Xcode: Xcode 9.0 Build version 9A235
  Android Studio: 2.3 AI-162.3934792

I recently ran into an issue which isn't really your fault, but might be a good feature request. In one project, I ran into this: npm: 2.15.12

Environment:
  OS: macOS High Sierra 10.13
  Node: 8.9.1
  Yarn: 1.3.2
  npm: 2.15.12
  Watchman: 4.9.0
  Xcode: Xcode 9.0 Build version 9A235
  Android Studio: 2.3 AI-162.3934792

Wait, wut? It turns out one of the dependencies in this project inexplicably has npm@2 as a dependency, and thus npx picks this up as the installation of npm, and prints its version out, even though 5.5.1 is installed globally.

So what that brings us to is a couple things.

  1. I have some work to do on my envinfo package.
  2. There may perhaps be some merit to a --prefer-global flag in npx? Your current hierarchy of which binary to run seems to be local => global => shell? (cache in there somewhere) but it would be excellent to be able to specify which to prefer. Sometimes with npx peeps may prefer a native shell solution first, then global, then local, or some combination.

Thanks!

ajcrites commented 7 years ago

For whatever reason, when I run npx cordova the order appears to be global => local, but in my case I'd prefer local. The ability to specify hierarchy when running commands makes sense to me.

Imo the order should be local => global, i.e. a locally installed version of the module should be favored even if a global is available. I also think the ability to specify the order that you want, or something like --prefer-global or --prefer-local would be very useful.

zkat commented 6 years ago

Hi! So what you're seeing here is a manifestation of a problem that I believe is really npm's: the fact that transitive dependencies have their binaries installed at the top level of the project. This is a pretty significant gotcha, and while npx is particularly vulnerable to it, it's the exact same problem you can run into when using npm run-script.

This turns out to be a duplicate of #127, and is linked to npm issue https://github.com/npm/npm/issues/18874, so I'm going to close this specific issue, but we can keep discussing it in one of the other places! Thanks for filing the issue: I'd definitely like to have this weird corner case fixed, myself.