'Yarn' is not recognized as an internal or external command

avatar
Borislav Hadzhiev

Last updated: Apr 4, 2024
4 min

banner

# 'Yarn' is not recognized as an internal or external command

To solve the error "'yarn' is not recognized as an internal or external command, operable program or batch file", install the yarn package globally by running npm install -g yarn, restart your terminal and make sure your PATH environment variable is set up correctly.

Open your terminal and install yarn globally by running the following command.

shell
# ๐Ÿ‘‡๏ธ install yarn CLI globally npm install -g yarn # ๐Ÿ‘‡๏ธ get the version yarn --version

If you see a package version output from the second command, then yarn is installed successfully.

If the global installation of yarn fails, you have to open CMD as an administrator and rerun the commands.

To open CMD as an administrator:

  1. Click on the Search bar and type CMD.

  2. Right-click on the Command Prompt application and click "Run as administrator".

run cmd as administrator

  1. Rerun the installation command.
shell
# ๐Ÿ‘‡๏ธ install yarn CLI globally npm install -g yarn # ๐Ÿ‘‡๏ธ get the version yarn --version

If the error is not resolved, try restarting your terminal.

If you got the "yarn command not found" error on macOS or Linux, click on the following article.

# Add npm to your PATH environment variable on Windows

To add npm to your PATH environment variable:

  1. Click on the Search bar and type "environment variables".
  2. Click on "Edit the system environment variables".

edit system environment variables

  1. Click on the "Environment Variables" button.

click environment variables

  1. In the "System variables" section, select the "Path" variable and click "Edit".

select path and click edit

  1. Click on "New" and then click "Browse".

click new browse

  1. Your npm directory is most likely located under %USERPROFILE%\AppData\Roaming\npm or in other words, C:\Users\YOUR_USER\AppData\Roaming\npm.
location
%USERPROFILE%\AppData\Roaming\npm # ๐Ÿ‘‡๏ธ same as below (make sure to replace YOUR_USER) C:\Users\YOUR_USER\AppData\Roaming\npm

If you can't find it, run the npm config get prefix command.

cmd
npm config get prefix

npm config get prefix

  1. Add the path to npm and click on "OK" twice to confirm.

  2. Close your Command prompt application and then reopen it.

Note that you must restart your Command prompt shell for the changes to take effect.

You might also have to restart your PC, but that's not always necessary.

  1. Run the following command to install the YARN CLI.
shell
# ๐Ÿ‘‡๏ธ install yarn CLI globally npm install -g yarn # ๐Ÿ‘‡๏ธ get the version yarn --version
If you get a permissions error when trying to install the YARN CLI globally, start CMD as an administrator and then run the commands.

To open CMD as an administrator:

  1. Click on the Search bar and type CMD.

  2. Right-click on the Command Prompt application and click "Run as administrator".

run cmd as administrator

If you get the error yarn.ps1 cannot be loaded because running scripts is disabled on this system, open your PowerShell as an administrator and set its execution policy with the Set-ExecutionPolicy command.

shell
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Make sure to open your PowerShell as an administrator before you run the Set-ExecutionPolicy command.

This effectively removes the execution policy of Restricted, which doesn't allow us to load configuration files or run scripts. The Restricted execution policy is the default for Windows client computers.

If the error persists, try to reinstall Node.js.

# Reinstalling Node.js

The easiest way to install Node and add it to your system's PATH environment variable is to:

  1. Open the nodejs.org page and download the Windows installer for the LTS (long-term supported) version.

download node lts version

  1. Start the installer and click Next on the Welcome screen.

node start installer

  1. Accept the End-User License Agreement and click Next.

node accept end user agreement

  1. Leave the default destination folder selected and click Next.

node select destination folder

  1. On the "Custom Setup" screen, click Next. Notice that there is an "Add to PATH" option.

node custom setup

  1. You can optionally install tools for native modules, otherwise, click Next.

node tools for native modules

  1. On the next screen, click on the Install button.

node click install

  1. Lastly, click on the Finish button.

node click finish

  1. Make sure to close any existing command prompts and PowerShell instances for the PATH variable to get updated.

  2. You can start a new CMD shell and use the npm --version command to make sure Node is installed.

cmd
npm --version node --version

get node npm version

  1. Run the following commands to install the YARN CLI.
cmd
# ๐Ÿ‘‡๏ธ install yarn cli globally npm install -g yarn # ๐Ÿ‘‡๏ธ get package version yarn --version
If the global installation of yarn fails, you have to open CMD as an administrator and rerun the commands.

Alternatively, you can see how you can fix the permissions error on this page in the official npm docs.

If you have difficulties installing and setting up Node.js correctly, try using NVM to manage your Node.js version.

Click on the article that relates to you and follow the instructions.

Want to learn more about installing and using NVM? Check out these resources: Install NVM on Windows,Install NVM on macOS and Linux.

# Additional Resources

I have two more articles on solving yarn-related errors:

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev