run `nvm use` automatically every time there's a .nvmrc file on the directory

前端 未结 10 1825
南笙
南笙 2020-12-23 11:05

How to configure my shell so that nvm use run automatically every time there\'s a .nvmrc file on the directory and use the latest version or a global config whe

10条回答
  •  半阙折子戏
    2020-12-23 11:20

    Yet another solution using direnv. Direnv comes with OS X and many distros so no installation is needed.

    Add these two lines to your .zshenv or .bash_profile depending on which shell you use:

    
    export NVM_DIR="$HOME/.nvm" # You probably have this line already
    export NODE_VERSIONS="${NVM_DIR}/versions/node"
    export NODE_VERSION_PREFIX="v"
    

    Add an .envrc file to the project root with the contents

    set -e
    use node
    

    Finally cd to your directory. (Don't forget to source .zshenv)

    direnv will ask you to allow load config. Type direnv allow and voila!

    Note that direnv doesn't support fancy constructs like lts/* in .nvrmc. On the positive side, direnv supports a bunch of runtimes like node, php, go, pyhton, ruby etc. allowing us to use a single tool to solve path issues.

提交回复
热议问题