npm
version 7 will cache the packages in a _npx
directory. It has a cache layout that apparently involves a hash. For example, for me npx shellcheck
installs the executable in ~/.npm/_npx/cca5ebdff9ce100b/node_modules/.bin/shellcheck
. (Note the cca5ebdff9ce100b
.) However, I very much doubt that the algorithm can be relied upon to be consistent across versions of npx
.
The whole point of npx
is that you can run the packages without installing them somewhere permanent. So I wouldn't use that cache location for anything. I wouldn't be surprised if cache entries were cleared from time to time. I don't know what algorithm, if any, npx
uses for time-based cache invalidation.
To get the location from which npx
runs the package, you can use -p
to tell it to install a package and then use which <executable>
or command -v <executable>
to get the path. So, for example, what I did above to get the location of the shellcheck
executable was to run npm -p shellcheck which shellcheck
or npm -p shellcheck command -v shellcheck
. Those commands assume a Linux or other UNIX-like operating system. I'm not sure what the equivalent for Windows would be.
$ npx -p shellcheck command -v shellcheck
Need to install the following packages:
shellcheck
Ok to proceed? (y)
/Users/trott/.npm/_npx/cca5ebdff9ce100b/node_modules/.bin/shellcheck
$
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…