As with Building a Python environment, this is a note from when I re-installed the Node.js environment on a new version of WSL's Ubuntu.
Since the version of Node.js used varies depending on when the JavaScript application was created, Node.js is installed using nvm, a Node.js version management tool, so that the version can be switched each time.
What is nvm?
It is like Python's pyenv, where you can install multiple versions of Node.js and switch between them at will.
How nvm works
As with pyenv, by rewriting where $PATH goes to look, it switches the version of Node.js that is invoked when you type "node".
Installation Procedure
See official website for details
Do the following
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
Then a ".nvm" folder will be created in the home directory, and the following will be appended to the ".bashrc".
export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
Re-launch WSL
The ".bashrc" will be loaded and "nvm" will be available.
When Node.js is installed using nvm, it is divided into folders by version and installed under ".nvm". Then, nvm changes the version of Node.js that is invoked when typing "node" by changing the destination of $PATH to the appropriate version of Node.js depending on the situation.
How to use nvm
List of Node.js versions that can be installed
nvm ls-remote
Install the specified version of Node.js
Example: Install version 10, 8, 6
nvm install 10 nvm install 8 nvm install 6
The detailed version number can be omitted.
List of installed versions
nvm ls
Default Node.js version to the specified version
Example: Default to version 10
nvm alias default 10
This way, the version when you launch WSL will be the one you specified.
Switching versions
Example: Switch to version 8
nvm use 8
Thereafter, the version of Node.js will be 8.
Restore version to default
nvm use default
Use Node.js installed on your system
The version name "system" is the Node.js installed on the system.
Example: Defaults to Node.js in the system
nvm alias default system
Set the Node.js version under the current directory to the specified version
First, record the version you wish to specify in the ".nvmrc" file below.
Example: Make version 6.
echo "6" > .nvmrc
If you do so, just type "nvm use" and the version of Node.js will be the one listed in ".nvmrc".
nvm use
uninstall
nvm uninstall <バージョン>
Other
Like pyenv, there is no function that automatically switches the Node.js version when you move to a directory, so you must manually "nvm use" to explicitly switch each time.
I have not used the tool avn, which was introduced to switch automatically, but I don't think it is necessary to go that far.
I've never really been aware of the version difference in Node.js since I end up using TypeScript as a cover for it, but it seems like major version upgrades are happening too frequently...