Schniz / fnm

🚀 Fast and simple Node.js version manager, built in Rust
https://fnm.vercel.app
GNU General Public License v3.0
17.33k stars 439 forks source link

Fresh install on macOS using Homebrew: shell looking in wrong INSTALL_DIR #1194

Open Scotchester opened 1 month ago

Scotchester commented 1 month ago

I apologize if I'm missing something obvious.

I just tried installing fnm on a fresh MacBook (running macOS Sonoma 14.5) using the default recommended installation script (curl -fsSL https://fnm.vercel.app/install | bash). I can see that this uses Homebrew to do the installation. The output looks pretty expected:

cranfill@MT-407147 ~ % curl -fsSL https://fnm.vercel.app/install | bash
Downloading fnm using Homebrew...
Checking dependencies for the installation script...
Checking availability of curl... OK!
Checking availability of unzip... OK!
Checking availability of Homebrew (brew)... OK!
==> Downloading https://formulae.brew.sh/api/formula.jws.json
#=#=#                                                                           
==> Downloading https://formulae.brew.sh/api/cask.jws.json
######################################################################### 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/fnm/manifests/1.37.1
Already downloaded: /Users/cranfill/Library/Caches/Homebrew/downloads/4ff5bc74da6dcb0f0d50fd4ab8d966015c9c47dce2cca73cfed4709cb35e5035--fnm-1.37.1.bottle_manifest.json
==> Fetching fnm
==> Downloading https://ghcr.io/v2/homebrew/core/fnm/blobs/sha256:b76925bd68d738
Already downloaded: /Users/cranfill/Library/Caches/Homebrew/downloads/c3a21558106709d5104b0af89a1cdb099d9bb40af6d236169bb755eddd8382f3--fnm--1.37.1.arm64_sonoma.bottle.tar.gz
==> Pouring fnm--1.37.1.arm64_sonoma.bottle.tar.gz
==> Caveats
zsh completions have been installed to:
  /opt/homebrew/share/zsh/site-functions
==> Summary
🍺  /opt/homebrew/Cellar/fnm/1.37.1: 12 files, 7.2MB
==> Running `brew cleanup fnm`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
Installing for Zsh. Appending the following to /Users/cranfill/.zshrc:

# fnm
FNM_PATH="/Users/cranfill/Library/Application Support/fnm"
if [ -d "$FNM_PATH" ]; then
  export PATH="/Users/cranfill/Library/Application Support/fnm:$PATH"
  eval "`fnm env`"
fi

In order to apply the changes, open a new terminal or run the following command:

  source /Users/cranfill/.zshrc

The problem is, the /Users/cranfill/Library/Application Support/fnm directory is not created, so the shell integration code does nothing.

cranfill@MT-407147 ~ % cd /Users/cranfill/Library/Application\ Support/fnm
cd: no such file or directory: /Users/cranfill/Library/Application Support/fnm

which fnm returns the default Homebrew location:

cranfill@MT-407147 ~ % which fnm
/opt/homebrew/bin/fnm

Reviewing the code in the install script, I don't see any situation in which the correct installation directory for a Homebrew install gets used in the shell setup code.

Scotchester commented 1 month ago

Looking at the recent changes to the installer, it appears that this may have have been introduced in #937. A simple fix might be to re-set INSTALL_DIR when doing a Homebrew installation, but you'd also want to avoid re-adding the Homebrew bin to the PATH, since it's already there.

jeegurda commented 1 month ago

I can confirm this does happen on the current homebrew version 1.37.1.

This is especially bad because if you follow the official guide from nodejs website, you'll end up with a borked installation:

# installs fnm (Fast Node Manager)
curl -fsSL https://fnm.vercel.app/install | bash
# download and install Node.js
fnm use --install-if-missing 20

The second command will install node in a wrong (default) directory, while your .zshrc will be setting the wrong path:

FNM_PATH="/Users/username/Library/Application Support/fnm"
if [ -d "$FNM_PATH" ]; then
  # ...
  # will not happen as the directory does not exist
gAmUssA commented 1 month ago

I have the same situation and getting error like

❱ fnm use
error: We can't find the necessary environment variables to replace the Node version.
You should setup your shell profile to evaluate `fnm env`, see https://github.com/Schniz/fnm#shell-setup on how to do this
Check out our documentation for more information: https://fnm.vercel.app

Thank you

ColCh commented 1 month ago

faced this as well

quick workaround fix is this one

cd ~
brew uninstall fnm
mkdir .fnm
curl -fsSL https://fnm.vercel.app/install | bash

now it says (i use fish shell):

Installing for Fish. Appending the following to /Users/colch/.config/fish/conf.d/fnm.fish:

# fnm
set FNM_PATH "/Users/colch/.fnm"
if [ -d "$FNM_PATH" ]
  set PATH "$FNM_PATH" $PATH
  fnm env | source
end

added that into config, and it works :) good luck!

got this by reading source code for install script: https://github.com/Schniz/fnm/blob/119ffc5961c578660f7c34206bc10c8e0eb5d227/.ci/install.sh#L12-L13