Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--use-on-cd should revert to default node #442

Closed
uloco opened this issue Apr 26, 2021 · 5 comments · Fixed by #607
Closed

--use-on-cd should revert to default node #442

uloco opened this issue Apr 26, 2021 · 5 comments · Fixed by #607

Comments

@uloco
Copy link

uloco commented Apr 26, 2021

The flag --use-on-cd should revert to default node if there is no .nvmrc present.
Is this already the case? Because it's just staying on the last used node version when switching in directories without one.

@zimme
Copy link

zimme commented May 5, 2021

The way I would want it is. If you navigate into a child folder (without a .nvmrc) of a parent folder with a .nvmrc file then the version of node specified in the parent folder will still be used.

However when navigating to the parent folder of the folder with a .nvmrc it should load the default if there is one or system if there is no default.

@betalb
Copy link

betalb commented Dec 16, 2021

This is how nodenv behaves
I've managed to implement zsh hook based on what is already provided by --use-on-cd. But it uses subshell so most likely not optimal. Native support of such behaviour from fnm use will be more efficient

autoload -U add-zsh-hook
_fnm_autoload_hook () {
    _node_config_path="$PWD"
    while [[ "$_node_config_path" != "" && ! -f "$_node_config_path/.nvmrc" && ! -f "$_node_config_path/.node-version" ]]; do
        _node_config_path=${_node_config_path%/*}
    done

    _node_current_alias="$(fnm current)"
    if [[ "$_node_config_path" != "" ]]; then
        _fnm_use_output="$(builtin cd -q "$_node_config_path" && exec fnm use 2>&1)"
    else
        _fnm_use_output="$(fnm use default 2>&1)"
    fi
    _node_updated_alias="$(fnm current)"
    if [[ "$_node_current_alias" != "$_node_updated_alias" ]]; then
        echo "$_fnm_use_output"
    fi
}

add-zsh-hook chpwd _fnm_autoload_hook \
    && _fnm_autoload_hook

rehash

Schniz added a commit that referenced this issue Dec 20, 2021
Adding the ability to provide `--version-file-strategy=recursive` on
both `fnm env` and on each command can allow us to support recursive
resolutions of the Node versions.

This makes the default Node version as easy as:

```bash
echo "16" > ~/.node-version
```

(assuming your code lives inside your home directory) and fixes #442, if
becomes the default.

---

This fixes #144 which is a very old issue 😃
#144 (comment)
@Schniz
Copy link
Owner

Schniz commented Dec 20, 2021

Sorry that I had no response. #607 is what I had in mind for this. Now that I think about it, I can add the default version if it finds nothing.

@nathanpovo
Copy link

nathanpovo commented Feb 24, 2023

Was this feature ever implemented?

Even though this issue is closed, the node version is not being changed if I cd to a directory that does not have an .nvmrc file.

Test in PowerShell:

image

Test in bash (just to make sure it is not due to #513):

image

Workaround

Create an .nvmrc file in the root directory of your drive with the version alias default as its content (rather than a specific version). This will cause fnm to recursively check each directory for an .nvmrc file until it reaches the root and then switches to the default node version.

image

@ashsearle
Copy link

Was this feature ever implemented?

Yes, but it requires an extra argument: --version-file-strategy=recursive. The README for this project probably needs updating to recommend this:

eval "$(fnm env --use-on-cd --version-file-strategy=recursive)"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants