How can I get yarn to use the right version of Node?

I am new to Node, JavaScript etc. and want to try messing with the code of Signal. For this I need a specific version of Node. I did this (on Ubuntu 16.04) using the following procedure:

wget https://nodejs.org/dist/v8.2.1/node-v8.2.1-linux-x64.tar.gz
sudo mkdir -p /opt/nodejs
sudo tar -xvzf node-v8.2.1-linux-x64.tar.gz -C /opt/nodejs/
cd /opt/nodejs/
sudo mv node-v8.2.1-linux-x64 8.2.1
sudo ln -s 8.2.1 current
sudo ln -s /opt/nodejs/current/bin/node /bin/node
mode -v

I then installed NPM:

sudo apt install npm

I then started working through the setup steps for Signal:

sudo npm install --global yarn
sudo yarn install --frozen-lockfile
sudo yarn grunt

When I run this last command, I see that an incorrect version of Node is being used and then encounter an error:

yarn run v1.5.1
warning You are using Node "4.2.6" which is not supported and may encounter bugs or unexpected behavior. Yarn supports the following semver range: "^4.8.0 || ^5.7.0 || ^6.2.2 || >=8.0.0"
error An unexpected error occurred: "Couldn't find a package.json file in \"/opt/nodejs\"".
info If you think this is a bug, please open a bug report with the information provided in "/opt/nodejs/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

What is going wrong?


You need to make sure you have nvm (Node Version Manager) installed and that it is being exported properly from your .bashrc file.

If you already have nvm installed, you can change the version to the node version you'd like by running nvm use v14.18.1

If you're using WSL or WSL2, this link should help you set it up. However, if you're using actual Ubuntu nvm should already be set up in the home directory in the .bashrc file.