40

I know I can change the node version by nvm use CLI command. However, I want to set specific node version differently for a certain project(folder). It's changed via nvm use command but it's reverted to default version whenever I restart the terminal or webstorm IDE.

How can I set nvm remember this different version for a certain project(folder)?

Nihal Razak
  • 403
  • 1
  • 4
  • 6

4 Answers4

48

You can use an .nvmrc file in the root of the project with the version you want to use. For example v12.4.0 or v10.16.0. You have to make sure that this version is installed or it will use the default node version in your machine.

DSCH
  • 2,146
  • 1
  • 18
  • 29
  • 16
    And run `nvm use` to trigger it pick up the version number from .nvmrc – Nitin Jul 08 '20 at 00:44
  • 6
    You can check [this answer](https://stackoverflow.com/a/50378304/276311) to automate the node version selection when you change to the project directory on bash. – Adriano P Mar 30 '21 at 15:52
7

For example, you want your default node version for this project to be v12.
Open your command line in the project root folder, then run nvm use 12, then run node -v > .nvmrc. It won't solve your issue completely because you'll anyway have to run nvm use just without the version.

Nadav
  • 1,730
  • 16
  • 20
0

One solution that I've found that works for me is creating an alias in your terminal of choice, in my case in .zshrc so that I can cd into the folder project and change the node version at the same time so I don't forget every time I get to that specific folder to change the node version with nvm. The alias looks like that:

alias foo='cd ~/route/to/project/folder && nvm use <node_version>'
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
ignblanch
  • 1
  • 2
0

One more method could be to use direnv. Create one file .envrc at the root of the folder where you want to use a different node version.

For example:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
nvm use <node_version>

Then run to allow using of this .envrc file.

direnv allow
  • First two lines just load the nvm, for some reason does not pick it up from .zshrc.
  • Second line would change the nvm version to the one you want to use.