Closed lydell closed 2 years ago
Forgot to search pull requests in addition to issues 🤦 Found this one right now: https://github.com/jorgebucaran/nvm.fish/pull/181
It is too slow... It prints stuff...
👌. We either need a plumbing nvm use
command or support this out of the box via a feature flag. Since there has been sustained support for this feature over the years, I'll look into taking a stab at this myself. I'll pull @edouard-lopez's #181 changes first then add my tweaks.
It fails if no
.nvmrc
or.node-version
is found. I want it to switch to the default version in that case – which issystem
for me.
Just curious, but why isn't your system Node always active?
Since there has been sustained support for this feature over the years, I'll look into taking a stab at this myself
Yay! I’m excited 😄 🎉
Just curious, but why isn't your system Node always active?
I usually have the latest LTS installed via brew
, and that’s the one I use generally. But then I have some older projects that need a specific older Node.js version (for the time being). In those I like to stick a .node-version
file. When entering one of those, I’d like to automatically switch to that Node.js version. When leaving the project folder (cd
away from it), I’d like to go back to the system Node.js that I usually use again, rather than stay on that older version just because I happened to be in that project folder before. Some things I thought about now:
~/.node-version
to achieve the same effect.brew
anymore. Feels easier to just install everything with nvm.fish!How about we introduce a --silent
flag that does the job silently, printing nothing. Then consider an official auto-switching feature on a different discussion? 🤓
nvm use --silent
+ me creating ~/.node-version
sounds like it should be all I need to implement automatic switching on my own, yes!
Edit: Oh wait, I’ll probably get errors when I (seldomly) go to, say, /usr
. Are the errors silenced too? Or should I create /.node-version
maybe? 🤔
Yes, but you can redirect to /dev/null in this case since errors won't slow you down as _nvm_node_info does.
Added a --silent
option flag that will suppress stdout (it works for nvm install
and nvm uninstall
too).
Please let me know if this should be all you need to implement automatic switching.
I added this to ~/.config/fish/config.fish
:
function __nvm_auto --on-variable PWD
nvm use --silent 2>/dev/null
end
__nvm_auto
And created ~/.node-version
with my desired default version.
nvm use --silent
seems to take like 9 ms.
It seems to work perfectly so far!* 🎉 Thank you!
9ms should do it. One could even get a bit fancier and put __nvm_auto
behind a feature flag (https://github.com/jorgebucaran/nvm.fish/pull/181#discussion_r885909453). 🤓
super fast and reliable so far! thx
Question: we nvm use $nvm_default_version
in new shells, but what if there is an .nvmrc file in that directory? Which one should have precedence: nvm_default_version or a local .nvmrc/.node-version file?
cc @lydell @denvis @edouard-lopez
a local .nvmrc/.node-version file should always win IMO. That’s how my current setup works. In other words, I want no difference between cd foo/
and starting out in foo/
from the start.
What happens when you move to a directory without a .nvmrc (after you've auto-switched versions)? Should you auto-switch to the previous one? Assume such a directory is not a subdirectory of the one that had the .nvmrc file.
Then I would like the default version. I’ve created ~/.node-version
with my desired default to simulate that.
Thank you, that makes sense and it's easy to do in userland too.
Hi!
First, thanks for nvm.fish! It’s so fast and easy to use.
Note: I made this as a PR rather than as an issue because it’s easier to show some things using code. I don’t expect this to be merged in its current (ugly, incomplete) form!
I read https://github.com/jorgebucaran/nvm.fish/issues/132 about the – now closed – suggestion about automatic version switching.
I get that it can be problematic. But I feel that for me, it can be more worth having automatic switching than perfection. So I started playing around with doing automatic switching myself:
However, there are a few problems with
nvm use
(with no arguments):node
call?) The slowness comes from_nvm_node_info
which invokesnode --eval
which isn’t the fastest./dev/null
though. I don’t need any prints, because my prompt shows the version. However, as mentioned in the above point, the prints are slow and/dev/null
does not help with performance..nvmrc
or.node-version
is found. I want it to switch to the default version in that case – which issystem
for me.I threw together some quick and bad modifications to
nvm.fish
to try things out. It looks promising!nvm use
now takes 8-12 ms to run, which feels acceptable to me on everycd
.system
if no.nvmrc
or.node-version
is found. (It should honor the default though, haven’t looked into that yet).What do you think? Would you be interested in having an
nvm use
optimized for this use case?Alternatively, any tips on how to use
nvm
effectively? Currently, I “fight” a lot with forgetting to switch version all the time. How do you do it? Would you recommend some other tool if this isn’t ”the nvm.fish philosophy”? (I’ve tried nodenv in the past, which has automatic switching, but it was so clunky to use.)