Reputation: 21
I installed node.js:
brew install node
I also installed asdf with node.js plugin. Is there any way to use node.js installed via Homebrew globally if there is no .tool-versions
file?
Right now I am getting an error if this file does not exist:
No version is set for command node
Consider adding one of the following versions in your config file at
nodejs 19.0.0
nodejs 19.0.1
nodejs 19.1.0
nodejs 19.2.0
I don't want to use asdf globally. Just for few projects.
Upvotes: 8
Views: 2189
Reputation: 2484
Using Bash you can use PROMPT_COMMAND to check if the .tool-versions
file exists in the current folder and add or remove .asdf
folders from the PATH
.
Just add this to your .bashrc
:
conditional_asdf() {
if [ ! -f "$PWD/.tool-versions" ]; then
export PATH=`echo $PATH | sed -E 's/(.*)\.asdf([^:]*)://'`
else
export PATH="${HOME}/.asdf/shims:${HOME}/.asdf/bin:${PATH}"
fi
}
PROMPT_COMMAND="conditional_asdf; $PROMPT_COMMAND"
Upvotes: 3
Reputation: 114
It doesn't completely answer your question, but I've stumbled upon the same problem and found the following workaround for using asdf
with some projects, but not others:
Instead of sourcing the asdf scripts for every shell, I wrap them in a condition. ~/.bashrc
(on Linux & Bash, in your case it will probably be ${ZDOTDIR:-~}/.zshrc
) might then contain the following:
# ASDF tool version manager
if [ -n "$WITH_ASDF" ]; then
. "$HOME/.asdf/asdf.sh";
. "$HOME/.asdf/completions/asdf.bash";
else
alias asdf="WITH_ASDF='true' /bin/bash";
fi;
With this asdf is initially not loaded at all and I can use NVM like before (or, in your case, the system's nodeJS). By simply typing asdf
into the terminal, a new shell is invoked with asdf loaded. To return to the non-asdf shell a simple exit
(or Ctrl+D) will do.
$> which node
/home/andi/.nvm/versions/node/v16.19.0/bin/node
$> asdf
$> which node
/home/andi/.asdf/shims/node
$> exit
(Obviously you'll have to adjust the above for your specific environment, i.e. Homebrew and probably ZSH, based on these code samples from asdf's getting started page.)
If you prefer, you could turn this principle around and make asdf the default (with some extra steps included that only apply if you use NVM) (untested):
# ASDF tool version manager
if [ -z "$NO_ASDF" ]; then
. "$HOME/.asdf/asdf.sh";
. "$HOME/.asdf/completions/asdf.bash";
# This alias could also be called "nvm"
alias noasdf="NO_ASDF='true' /bin/bash";
# This "else" block is optional if you only want to use the system's nodeJS
else
# Node version manager
export NVM_DIR="$HOME/.nvm";
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm;
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"; # This loads nvm bash_completion
fi;
You could probably build on that and have a script auto-evaluate whether a .tool-versions
file exists in the cwd at any given time and then automatically start or exit the shell, but that's quite a bit beyond my shell-fu.
Upvotes: 0