为什么当我将项目中的目录更改为“node_modules”时,nvm 会自动切换版本?目录中没有“.nvmrc”文件

问题描述 投票:0回答:1

查看此屏幕录制:

运行

npm install
然后将目录更改为
node_modules
文件夹后,
nvm
会自动将我切换到系统上的默认节点版本(v20.5.1)。为什么会发生这种情况?目录中没有
.nvmrc
文件。这只是
nvm
的默认行为吗?

我期待我的

nvm use 18
声明能够坚持下去。

如果有人能解释为什么会发生这种情况,我们将不胜感激。

node.js ubuntu nvm
1个回答
0
投票

罪魁祸首是

.bashrc
中的别名和相应的函数:

cdnvm() {
    command cd "$@" || return $?
    nvm_path=$(nvm_find_up .nvmrc | tr -d '\n')

    # If there are no .nvmrc file, use the default nvm version
    if [[ ! $nvm_path = *[^[:space:]]* ]]; then

        declare default_version;
        default_version=$(nvm version default);

        # If there is no default version, set it to `node`
        # This will use the latest version on your machine
        if [[ $default_version == "N/A" ]]; then
            nvm alias default node;
            default_version=$(nvm version default);
        fi

        # If the current version is not the default version, set it to use the default version
        if [[ $(nvm current) != "$default_version" ]]; then
            nvm use default;
        fi

    elif [[ -s $nvm_path/.nvmrc && -r $nvm_path/.nvmrc ]]; then
        declare nvm_version
        nvm_version=$(<"$nvm_path"/.nvmrc)

        declare locally_resolved_nvm_version
        # `nvm ls` will check all locally-available versions
        # If there are multiple matching versions, take the latest one
        # Remove the `->` and `*` characters and spaces
        # `locally_resolved_nvm_version` will be `N/A` if no local versions are found
        locally_resolved_nvm_version=$(nvm ls --no-colors "$nvm_version" | tail -1 | tr -d '\->*' | tr -d '[:space:]')

        # If it is not already installed, install it
        # `nvm install` will implicitly use the newly-installed version
        if [[ "$locally_resolved_nvm_version" == "N/A" ]]; then
            nvm install "$nvm_version";
        elif [[ $(nvm current) != "$locally_resolved_nvm_version" ]]; then
            nvm use "$nvm_version";
        fi
    fi
}

alias cd='cdnvm'
cdnvm "$PWD" || exit

我想我一定是在安装NVM时添加了它,然后忘记了。删除函数和别名解决了问题。

© www.soinside.com 2019 - 2024. All rights reserved.