26

If I'm using npx to run a binary as a one-off, it'll output the following:

npx my-module 

/// npx: installed 1 in 1.34s
/// Hello, from my module!

Where are these binaries stored by default? Does npx save the binaries after execution, a-la npm or does it just run them and then remove the files?

It's my understanding that npx will first look in the local node_modules/.bin diectory and then the /usr/local/bin directory, before it downloads the module. But I've checked both of those locations and don't see the new module...

Harrison Cramer
  • 3,792
  • 9
  • 33
  • 61
  • 1
    usr/local/bin not .bin – 0xLogN Apr 09 '21 at 22:22
  • Thanks, I'm still not seeing any of the npx binaries in there. Everything is running fine. Just curious where the files are installed on my system – Harrison Cramer Apr 09 '21 at 22:36
  • Could be in ~/.cache or ~/.local? – 0xLogN Apr 09 '21 at 22:46
  • Try this: `npx SOME_PACKAGE_WITH_BINARY_THAT_ISNT_INSTALLED_LOCALLY && find / -type f -name 'SOME_PACKAGE_WITH_BINARY_THAT_ISNT_INSTALLED_LOCALLYS_BINARY'`. – 0xLogN Apr 09 '21 at 22:47
  • 1
    > Try this: npx SOME_PACKAGE_WITH_BINARY_THAT_ISNT_INSTALLED_LOCALLY && find / -type f -name 'SOME_PACKAGE_WITH_BINARY_THAT_ISNT_INSTALLED_LOCALLYS_BINARY' Do this instead: `npx -p SOME_PACKAGE which SOME_PACKAGE` or `npx -p SOME_PACKAGE command -v SOME_PACKAGE`. That will be much faster than using `find` to search through the entire filesystem for the executable. – Trott Apr 10 '21 at 04:10

2 Answers2

38

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 npx -p shellcheck which shellcheck or npx -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
$ 
Trott
  • 66,479
  • 23
  • 173
  • 212
3

In Windows, this would be: C:\Users\{YourUserName}\AppData\Local\npm-cache\_npx\

or the bash shell equivalent of: ~/Local Settings/npm-cache/_npx/ etc.

jaygeek
  • 1,004
  • 10
  • 14