7

For a build process that requires updating from Node.js 12 to 14, I'd like a bash script to detect whether nvm is installed, and, if so, do nvm use v14 (or nvm install v14 if necessary), and then I want the nvm-selected node version to stick at 14 after the bash script terminates, not just for the duration of the script.

I can switch to v14 with this script, but after the script has terminated, the shell environment remains at v12:

#!/bin/bash

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
source ~/.bashrc

nvm --version
echo $NVM_BIN
node --version
nvm use v14
echo $NVM_BIN
node --version

Just executing the nvm command in a bash script is a pain because nvm isn't a true command, but a shell function, and the script has to use the first three lines to set up nvm for the script.

The output is:

0.33.11
/home/pi/.nvm/versions/node/v12.21.0/bin
v12.21.0
Now using node v14.16.0 (npm v6.14.11)
/home/pi/.nvm/versions/node/v14.16.0/bin
v14.16.0

When the script is finished, however:

enter image description here

I think the trick might be making sure the environment variable NVM_BIN persists at the v14 path when the script exits, but I don't know how to do that. In fact, I think it's generally not allowed for the shell in which a script executes to change environment variables in the parent shell.

The nvm commmand, however, is itself a shell script, and whatever it does is persistent after it's done executing. There should be some way for me to make this happen too.

kshetline
  • 12,547
  • 4
  • 37
  • 73

2 Answers2

1

I think if you set an alias with nvm for default,

nvm alias default 14.0.0

the node version will persist across any new terminal instances.

pritam
  • 2,452
  • 1
  • 21
  • 31
  • 1
    Looked good for a moment there... but sadly, it didn't work. – kshetline Feb 25 '21 at 16:35
  • Well... maybe. If I close the terminal I was in, and open the terminal again, then the new default kicks in. That _might_ be good enough, as long as another process started up from the main script inherits this change too, without exiting the original terminal. You at least deserve an up-vote for at least getting me part way. :) – kshetline Feb 25 '21 at 16:39
  • 1
    You can probably try the options mentioned here - https://stackoverflow.com/questions/47190861/how-can-the-default-node-version-be-set-using-nvm – pritam Feb 25 '21 at 16:42
  • 4
    Not sure, why this was marked as a solution, but it's definitely not addressing the core of the problem. I'm also wondering how to call the `nvm` instance of the terminal which invoked the script, and persist the changed node version *particularly* in that instance. Updating the system-wide value for every new instance - not an option – Skyrocker Jan 18 '22 at 11:06
0

To have the selected node version after the script executes you can run it like this:

. change-node-version.sh

script

node --version
nvm use 14.18.0
node --version

output

then:

node --version
➜  v14.18.0
Jorge Tovar
  • 1,374
  • 12
  • 17