Search code examples
npmyarnpkgwindows-subsystem-for-linux

Install yarn and npm in WSL


I have installed Windows Subsystem for Linux (WSL) and Ubuntu 16.04 on my Windows 10. Then I followed this to install yarn:

sudo apt update
sudo apt install curl
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update
sudo apt install yarn

It did not raise any error. However, yarn --version returned

$ yarn --version
/mnt/c/Users/chengtie/AppData/Roaming/npm/yarn: 12: /mnt/c/Users/chengtie/AppData/Roaming/npm/yarn: node: not found

npm --version returned

$ npm --version
: not foundram Files/nodejs/npm: 3: /mnt/c/Program Files/nodejs/npm:
: not foundram Files/nodejs/npm: 5: /mnt/c/Program Files/nodejs/npm:
/mnt/c/Program Files/nodejs/npm: 6: /mnt/c/Program Files/nodejs/npm: Syntax error: word unexpected (expecting "in")

Does anyone know how to fix this?


Solution

  • Look at this:

    /mnt/c/Users/chengtie/AppData/Roaming/npm/yarn: 12
    

    It is looking for the yarn installed on Windows. You must uninstall yarn from windows or remove it from the WSL's PATH.

    Use the following to remove windows yarn from the PATH:

    WIN_YARN_PATH="$(dirname "$(command -v yarn)")"
    export PATH="$(echo "${PATH}" | sed -e "s#${WIN_YARN_PATH}##")"
    

    You can add this to your .bashrc

    Also, look at your error messages it is getting also npm from windows, you need to remove them from the path using the same technique.