For the Node.js world, we can use NVM to switch between Node.js versions, and also switch out which global packages are in scope (in the $PATH).
However, what if I want to ensure that I use typescript version 3.3.334 at the command line? Obviously, I could install that version of typescript to my local project, but if I wanted to avoid a local dependency, how can I use a shell program to use an exact version of typescript at the command line?
I am thinking something like this:
package="[email protected]"
dir="$HOME/.temp/$package";
if [ ! -d "$dir" ]; then
mkdir -p "$dir"
(cd "$dir" && npm i "$package")
fi
export PATH="$dir/node_modules/.bin:$PATH"
do_the_original_thing_you_wanted_to_do foo bar
I will probably end up writing some shell script or bash function to switch versions of global cli tools easily, this works:
all you have to do to generify it is have some arguments for the package and version. this technique should work with compiled/native-code packages also, I don't foresee a problem with that.