Ran into needing this when I switched to using the WSL shell halfway through a project and then realised that (of course!) it’s an entirely different environment that I hadn’t necessarily brought up to speed since installing ages ago. So:
> sudo apt-get purge nodejs
Then clean up dependencies left behind:
> sudo apt-get autoremove
Get rid of the node source list:
> ls /etc/apt/sources.list.d
> sudo rm /etc/apt/sources.list.d/nodesource.list
Now, “which node” should return nothing:
> which node
>
Now install node using the instructions here summarised as:
> sudo apt-get install curl
> curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.2/install.sh | bash
CLOSE YOUR WSL TERMINAL and reopen first, then verify - these should work else re-attempt the curl ops. (nvm ls shoul return no nodes; we haven’t installed it yet):
> command -v nvm
> nvm --version
> nvm ls
Continue:
> nvm install node
Verify installs (including npm, which u should also get with the install)
> node --version
> npm --version
Hereafter, whenever you cd into a project, you can do:
nvm ls-remote
to see what versions of npm are available that you can install.nvm use [--lts | v0.0.0]
to use latest stable or specific version.