10

I have problem with installing node.js and npm on my Ubuntu terminal (WSL2).

I tried to follow this instructions:

https://learn.microsoft.com/en-us/windows/dev-environment/javascript/nodejs-on-wsl

https://github.com/MicrosoftDocs/windows-uwp/blob/docs/hub/dev-environment/javascript/nodejs-on-wsl.md

Also see some videos on YouTube, but every time I have same error.

First, I run this command:

sudo apt-get install curl

after I am trying to install nvm, with this command :

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

after that it tells, that nvm is installed, but shows some errors(I will include img) and after that, I can't run this command to verify versions of node.js and npm :

command -v nvm  //or this command 
nvm ls

I already installed node.js in my windows, and when I open git bash and run this commands node -v and npm -v it shows me, which versions are installed. But as I use Ubuntu terminal with fish, I wanted to install node.js and npm on it too. Can someone tell me what I am doing wrong?

enter image description here

2 Answers 2

18

It looks like it is installed. All OP needs to do is restart the terminal.

In my case, I've installed nvm with this instead

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash

Then restarted the terminal, and it is working.

Below will share the full process I've been through to install Node.js in WSL.


First of all, make sure one has WSL installed by following this. In this case OP already did that, so, just in case, would update the system's package

sudo apt-get update

Then, the following will install Node.js on WSL:

  1. Install cURL (a tool used for downloading content from the internet in the command-line)

    sudo apt-get install curl
    
  2. Install nvm (there are alternatives to nvm - see the first note below) (Source)

    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash
    

    Verify the installation with command -v nvm (it should output nvm)

    enter image description here

    Check Node versions available with nvm ls.

  3. Install Node.js.

    There are various options to do that. Here will only consider installing:

    a) Current stable version (recommended for PRD):

    nvm install --lts
    

    enter image description here

    b) Current release:

    nvm install node
    
  4. Check Node versions available

    nvm ls
    

    enter image description here

  5. Verify Node.js installation node --version (or node -v).

    enter image description here

  6. Verify npm installation with npm --version

    enter image description here


Notes

Sign up to request clarification or add additional context in comments.

3 Comments

All OP needs to do is restart the terminal. (that worked for me)
You are a life server. I was skeptical, but nvm is the only thing that helped me to use a Typescript Node.js server in WSL2. Without nvm it seems that the normal Windows npm command was taking over, and yielding many strange & uninformative errors.
I was stuck getting a specific package to cooperate on Windows, and I couldn't even get WSL to work... until now. Thank you.
2

I believe this may be because you need to restart your terminal, this is one of the troubleshooting tips given from the NVM github page.

There are not any errors when downloading so it is probably just that the .bashrc file was updated but not ran (you should be able to use source ~/.bashrc instead of restarting)

1 Comment

after entering : source ~/.bashrc; it throw error : ~/.bashrc (line 6): 'case' builtin not inside of switch block case $- in ^ from sourcing file ~/.bashrc source: Error while reading file '/home/rostorm/.bashrc' I also restart Terminal ,but same problem . when I enter command -v nvm command nothing happens and nvm ls throws error Command 'nvm' not found

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.