Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

566
Visualizações
How to write a .nvmrc file which automatically change node version

Hi I have two projects one in angularjs 4.4.7 and another in angular 6 version. I need to switch between node version for this. I tried using NVM which is working manually. How to handle the version change inside the angularjs program to change the node version when automatically the latest angular page gets loaded. Is there a possible way like that. I went through the #avn also but how to create the .node-version file. Can someone help with any link or correct sample steps

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

As @Aditya-M-P has already mentioned you can run the following command inside your projects root directory to generate the .nvmrc to set a desired NodeJS version for you project to work properly:

node -v > .nvmrc

It will generate something like this inside your .nvmrc file:

v10.16.2

Also using 10.16.2 without the v letter will work just fine.

However, in the official documentation in the .nvmrc section it never mentions that once you get this file created, the specified node version will be loaded automatically. So that's not enough, you need to run the command below so that nvm can look for the .nvmrc file to load the specified version:

nvm use

Here it is a gif for demoing purpose: enter image description here

To autoload the specified node version:

You need to add something else to your shell configuration depending on what you use bash or zsh

To get the exact configuration for each of them, please follow the instructions in the corresponding shell config section.

In my case I'm using zsh so I do need to add this at the end of my .zshrc file and here is the image that confirms it works like a charm:

# place this after nvm initialization!
autoload -U add-zsh-hook
load-nvmrc() {
  local node_version="$(nvm version)"
  local nvmrc_path="$(nvm_find_nvmrc)"

  if [ -n "$nvmrc_path" ]; then
    local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")

    if [ "$nvmrc_node_version" = "N/A" ]; then
      nvm install
    elif [ "$nvmrc_node_version" != "$node_version" ]; then
      nvm use
    fi
  elif [ "$node_version" != "$(nvm version default)" ]; then
    echo "Reverting to nvm default version"
    nvm use default
  fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc

enter image description here

I hope it could be useful for anyone else facing the same question! 😎

over 4 years ago · Santiago Trujillo Relatório

0

As pointed out in the GitHub issue thread related to this on the nvm repository, you may run the following command in each of your Angular project folders:

$ node -v > .nvmrc

Note that you need to first switch to the right version of node in each of your projects, before running the command above.

What's happening in the command:

  • node -v will out the current version of node to stdout.
  • The > symbol will then redirecting the output to a file called .nvmrc (it will overwrite if something already exists with the same file name).
  • Read more bash redirections under the REDIRECTION section under the bash man page: https://linux.die.net/man/1/bash

When you cd into your target directories, nvm will now first read the file, and auto-switch to the correct version.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda