fnm icon indicating copy to clipboard operation
fnm copied to clipboard

Node not switching

Open selrond opened this issue 2 years ago • 32 comments

I've installed fnm on mac using homebrew, and put eval "$(fnm env)" inside my .zshrc. It appears to work, but when I run fnm use <version>, and test that version using node -v, output versions are different, as if fnm didn't switch it at all.

It only works after I re-source .zshrc. I guess it's not supposed to work that way...

Is there anything I have to do to make it work that I overlooked?

selrond avatar Nov 23 '21 08:11 selrond

hmmm

can you please share your $PATH env var? :)

Schniz avatar Nov 23 '21 08:11 Schniz

/usr/local/sbin:/var/folders/_r/slz2zc7s2hs59_8rtc3lww900000gn/T/fnm_multishells/13700_1637656413932/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/MacGPG2/bin:/Library/Apple/usr/bin

selrond avatar Nov 23 '21 12:11 selrond

It's weird, because when I launch VSCode from the terminal, (so it uses the proper node version) and check the node version from within it (using its integrated terminal), it outputs proper version.

Only the current terminal session seems to not be updating

selrond avatar Nov 23 '21 12:11 selrond

Maybe there's a need for rehash? Can you show me:

which node

$FNM_MULTISHELL_PATH/bin/node --version

Schniz avatar Nov 23 '21 12:11 Schniz

image

selrond avatar Nov 23 '21 16:11 selrond

try running rehash after fnm use 14 please :) just to make sure that it works

Schniz avatar Nov 23 '21 17:11 Schniz

yep, that seems to fix it - do I have to run it every time after changing the version though?

image

selrond avatar Nov 23 '21 17:11 selrond

I need to check why this is happening. It did not happen to me and I'm using Zsh, maybe that's a newer version and we need to fix fnm somehow.

Schniz avatar Nov 23 '21 17:11 Schniz

sure, if I can be of any help, let me know

selrond avatar Nov 23 '21 17:11 selrond

@Schniz it appears to be fixed since https://github.com/Schniz/fnm/releases/tag/v1.28.1

I've taken a look at the PR, and although I don't know a thing when it comes to Rust, maybe this line is related? (based on hashmap keyword)

Anyway, let's close this and I'll report back if I'll encounter it again

selrond avatar Dec 03 '21 20:12 selrond

weird, it should not affect the shell binary resolution 🤔 for some reason, rehashing is required sometimes. I don't know why, but I have an idea how to work it out and fix other issues with the same fix :)

closing for now :)

Schniz avatar Dec 20 '21 11:12 Schniz

@Schniz I’ve started to experience this behavior again... 😕

selrond avatar Feb 03 '22 08:02 selrond

Could you show echo $PATH and hash -t node?

alexeyten avatar Feb 03 '22 09:02 alexeyten

PATH:

/usr/local/sbin:/Users/selrond/Library/Caches/fnm_multishells/85078_1644225259663/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin

hash -t node throws hash: bad option: -t

selrond avatar Feb 07 '22 09:02 selrond

Oh, you use zsh. Then it's hash -L | grep node= command.

alexeyten avatar Feb 07 '22 09:02 alexeyten

thx, here it is:

hash node=/Users/selrond/Library/Caches/fnm_multishells/4390_1644223494403/bin/node

selrond avatar Feb 07 '22 12:02 selrond

@alexeyten do you know of a solution to this problem?

selrond avatar Feb 15 '22 07:02 selrond

Nope. I don't use zsh and macos.

alexeyten avatar Feb 15 '22 07:02 alexeyten

I need to check why this is happening. It did not happen to me and I'm using Zsh, maybe that's a newer version and we need to fix fnm somehow.

@Schniz Any chance I could help to pin this issue down?

selrond avatar Feb 15 '22 20:02 selrond

There’s a hack I researched. Rehashing on zsh automatically. I’ll open a PR tomorrow and then you can test it before merging. Wdyt?

Schniz avatar Feb 15 '22 20:02 Schniz

I’m no expert in zsh, so whatever works 😄

I’ve noticed a strange(?) behavior - once I run rehash once in a terminal session, it seems to be working (tried switching versions back and forth and running node -v)

selrond avatar Feb 16 '22 07:02 selrond

However, this is no longer a case:

when I launch VSCode from the terminal, (so it uses the proper node version) and check the node version from within it (using its integrated terminal), it outputs proper version

where node: output from main terminal

/Users/selrond/Library/Caches/fnm_multishells/3981_1644995919375/bin/node
/opt/homebrew/bin/node

VSCode terminal after launching it

/opt/homebrew/bin/node
/Users/selrond/Library/Caches/fnm_multishells/3981_1644995919375/bin/node
/opt/homebrew/bin/node

after rehash:

/Users/selrond/Library/Caches/fnm_multishells/5549_1644996562545/bin/node
/opt/homebrew/bin/node
/Users/selrond/Library/Caches/fnm_multishells/3981_1644995919375/bin/node
/opt/homebrew/bin/node

selrond avatar Feb 16 '22 07:02 selrond

I do not use VSCode so it'll be hard for me to debug & verify. I don't know what it's running in the integrated shell, etc.

rehash makes sense, this is why we're doing it 🤔 https://github.com/Schniz/fnm/blob/1acb2892c502c73e4acd0469410f1b8ffa687678/src/shell/zsh.rs#L26-L28

Schniz avatar Feb 16 '22 07:02 Schniz

my idea was to call rehash before calls to node or before any call (yet this feels... not good 😁)

Schniz avatar Feb 16 '22 07:02 Schniz

I’ve solved the VSCode problem by not running its terminal as a login shell, so that’s out of question.

Only the rehash-needed problem remains

selrond avatar Feb 16 '22 07:02 selrond

I’ve "solved" it for now by uninstalling system nodejs (installed via homebrew). Not exactly happy with the solution, but it seems to work. Really weird, I’ve never came across rehash previously, and I’ve been tinkering with terminal setup for some years now...

selrond avatar Mar 07 '22 14:03 selrond

Not sure if this is related. Is it possible to save the node version preference once I quit the current terminal session?

I have a default node version (v12) but I would want to use (v16) in my project. After setting v16 with fnm use, if I access the project from another terminal window or close the terminal and open it again in my project folder, the node version is reset to v12.

Anybody have a clue on how I can solve this?

edgarkanyes avatar Mar 08 '22 11:03 edgarkanyes

I'm facing the same issue with bash 5.1.16 and fnm 1.31.0, specifically when switching between the system node version and one managed by fnm. The output of which node will show the correct path in both cases, but the path will be cached to the previous location until I manually run hash -d node (after which it works as expected). Switching between fnm managed versions is fine because the path is still /run/user/1000/fnm_multishells/.../bin/node after switching, the problem only comes when running fnm use system because the path changes to /usr/bin/node in that case.

sangaline avatar Apr 11 '22 14:04 sangaline

I have same issue here. I'm using zsh 5.8.1 and fnm 1.29.2. The problem only appears when the system nodejs is involved. When I currently use the system node it works perfectly fine. But when I change the node version (either manually fnm use or when I cd to a directory with .node-version) it node version does not work. which node shows proper path (like /tmp/fnm_multishells/7982_1649893314783/bin/node) but node still uses the system node (/usr/bin/node checked through hash -m "node").

peeeyow avatar Apr 13 '22 23:04 peeeyow

I also have the same problem. When switching node version from system to v14, the version doesn't change unless rehash is called. However, after calling rehash, switching back to system then v14 will switch the version.

177051111-700919db-2a2a-4a1f-922b-4546b2f958b6

boompikachu avatar Jul 03 '22 17:07 boompikachu