devsetup1-6 Exercise 2: nvm and Per-Project Node Versions ========================================================== STEP 1: INSTALL nvm (the clone method) -------------------------------------- sudo apt install git curl # if not already installed (Chapter 2) git clone https://github.com/nvm-sh/nvm.git ~/.nvm cd ~/.nvm && git checkout v0.40.8 Append to ~/.bashrc: export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" Open a new terminal, then: command -v nvm # prints: nvm nvm --version STEP 2: INSTALL THE LTS AND LOOK AT THE PATH -------------------------------------------- nvm install --lts nvm alias default 'lts/*' node --version type -a node type -a node should now report a path under ~/.nvm/versions/node/vXX.YY.Z/bin/, for example /home/you/.nvm/versions/node/v24.x.y/bin/node. There should be no /usr/bin/node unless you installed the Debian package as well. The first entry is the one that runs. STEP 3: A SECOND VERSION AND TWO PROJECTS ----------------------------------------- nvm install 22 mkdir -p ~/projects/proj-a ~/projects/proj-b echo 24 > ~/projects/proj-a/.nvmrc echo 22 > ~/projects/proj-b/.nvmrc cd ~/projects/proj-a && nvm use && node --version # v24.x.y cd ~/projects/proj-b && nvm use && node --version # v22.x.y nvm use with no argument reads .nvmrc in the current directory. It prints a line such as "Now using node v22.x.y". If a version named in .nvmrc is not installed, nvm says so and suggests "nvm install" (with no argument, that installs the version .nvmrc names). Also show: nvm ls # both versions listed; an arrow marks the active one nvm current WHAT TO NOTICE -------------- - Nothing here needed sudo, and nothing was written outside your home directory. - Switching changes which directory is first on PATH, which you can confirm with type -a node after each nvm use. - The .nvmrc file is what makes the choice repeatable; commit it with the project. WHY THIS WORKS AS AN ANSWER --------------------------- It reproduces the mechanism (PATH ordering) rather than just the commands, and shows the per-project pinning that is the reason to use a version manager at all.