关于npx与pnpm搭配使用及本地二进制文件执行的技术咨询
Hey there! Let's clear up your confusion around npx and pnpm:
First off, using npx directly with packages installed via pnpm usually doesn't work smoothly. That's because npx is tied to npm's ecosystem by default—it checks npm's cache or remote repositories first, and doesn't automatically recognize the local binary paths that pnpm uses.
You're totally right about pnpx behaving more like npm create by default—it prioritizes fetching remote packages to execute instead of using locally installed ones. But pnpm does have dedicated tools to run local binaries, and a few workarounds if you need specific behavior:
Use
pnpm exec(the official pnpm equivalent)
This is the go-to command for running locally installed binaries with pnpm. For example, if you haveeslintinstalled in your project, just runpnpm exec eslint .—it'll directly use the local version without reaching for remote resources. It's designed specifically for this use case, so it's way more reliable than trying to force npx to work with pnpm.Leverage
pnpxwith a flag
If you still want to usepnpx, you can add the--no-installflag:pnpx --no-install <package-name>. This tells pnpx to only look for locally installed packages instead of downloading remote ones. It works, butpnpm execis more intuitive for this scenario.Manual path workaround for npx (not recommended)
If you really need to use npx with pnpm's local binaries, you can manually point to the binary in yournode_modules/.bindirectory, likenpx ./node_modules/.bin/eslint. This works, but it's clunky and not ideal for regular use.
If you're working with scripts defined in your package.json, you can also just use pnpm run <script-name>—pnpm will automatically use the local binaries referenced in those scripts.
备注:内容来源于stack exchange,提问作者Matt Sanders

