首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >每当目录中有.nvmrc文件时,自动运行` `nvm use`

每当目录中有.nvmrc文件时,自动运行` `nvm use`
EN

Stack Overflow用户
提问于 2014-05-09 11:56:29
回答 10查看 66K关注 0票数 113

如何配置我的shell,使nvm use在每次目录中有.nvmrc文件时自动运行,并在没有.nvmrc文件时使用最新版本或全局配置?

EN

回答 10

Stack Overflow用户

回答已采纳

发布于 2014-05-09 12:13:59

我刚刚发现了Node.js https://github.com/wbyoung/avn的自动版本切换,你可以用它。

你也可以关注这个线程https://github.com/creationix/nvm/issues/110

票数 31
EN

Stack Overflow用户

发布于 2020-05-30 05:49:00

还有另一个使用direnv的解决方案。Direnv自带OS X和许多发行版,因此不需要安装。

根据您使用的shell,将这两行添加到.zshenv或.bash_profile中:

代码语言:javascript
复制
export NVM_DIR="$HOME/.nvm" # You probably have this line already
export NODE_VERSIONS="${NVM_DIR}/versions/node"
export NODE_VERSION_PREFIX="v"

将包含以下内容的.envrc文件添加到项目根目录

代码语言:javascript
复制
set -e
use node

最后,cd进入你的目录。(别忘了源码.zshenv)

direnv将询问您是否允许加载配置。输入direnv allow,瞧!

请注意,direnv不支持像.nvrmc中的lts/*这样的奇特结构。从积极的方面来看,direnv支持一系列的运行时,比如node、php、go、pyhton、ruby等等,允许我们使用单一的工具来解决路径问题。

票数 3
EN

Stack Overflow用户

发布于 2021-12-22 07:20:21

在使用fish shell时,只要目录中有.nvmrc,就可以运行nvm use

代码语言:javascript
复制
# TODO: save this as `$HOME/.config/fish/conf.d/use_nvmrc.fish` 

# HOW IT WORKS
# `nvm use` whenever .nvmrc is present in $PWD when using fish shell
# when traveling deeper, use the parent .nvmrc unless otherwise set
# also go back to default nvm when leaving the nvmrc-specified zone

function set_nvm --on-event fish_prompt
    # runs whenever the fish_prompt event occurs
    # if the current directory hasn't changed, do nothing
    string match -q $PWD $PREV_PWD; and return 1

    # if the current directory is within the previous one where we found an nvmrc
    # and there is no subsequent .nvmrc here, do nothing, we are in the same repo
    string match -eq $PREV_PWD $PWD; and not test -e '.nvmrc'; and return 1

    # if we clear those checks, keep track of where we are
    set -g PREV_PWD $PWD

    if test -e '.nvmrc'

        # if we find .nvmrc, run nvm use
        nvm use

        # and remember that we used that node
        set NVM_DIRTY true

    else if not string match $NVM_DIRTY true

        # if we have set nvm and have stepped out of that repo
        # go back to default node, if not already on it
        not string match -eq (nvm current) (nvm alias default); and nvm use default

        # and clear the flag
        set NVM_DIRTY
    end
end
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23556330

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档