Home//

How to Fix node: command not found

How to Fix node: command not found

Minh Vu

By Minh Vu

Updated Aug 12, 2023

If you're a programmer who frequently works with Node.js, you may have encountered the frustrating "node: command not found" error. This error occurs when your system cannot locate the Node.js executable, preventing you from running any Node.js commands. In this tutorial, we'll explore why this error occurs and provide several solutions to fix it.

Why node: command not found error occurs

The "node: command not found" error typically occurs due to one of the following reasons:

  1. Node.js is not installed: If Node.js is not installed on your system, you won't be able to run any Node.js commands. Make sure you have Node.js installed before proceeding with the solutions.

  2. Incorrect PATH configuration: The PATH environment variable is responsible for locating executables on your system. If the PATH is not configured correctly, your system won't be able to find the Node.js executable.

  3. Node.js installation is corrupted: In some cases, the Node.js installation may be corrupted, leading to the "node: command not found" error. Reinstalling Node.js can help resolve this issue.

Now that we understand why this error occurs, let's dive into the solutions to fix it.

Solution 1: Check Node.js Installation

The first step is to ensure that Node.js is installed on your system. Open your terminal and run the following command:

shell
node -v

If Node.js is installed, you should see the version number printed on the screen. For example, v14.17.3. If you see an error message or nothing is printed, it means Node.js is not installed.

To install Node.js, you can follow the official installation guide for your operating system. Here are a few examples:

  1. Windows: Visit the Node.js website and download the Windows installer. Run the installer and follow the on-screen instructions to complete the installation.

  2. macOS: You can use Homebrew to install Node.js. Open your terminal and run the following command:

    shell
    brew install node
  3. Linux: The installation process varies depending on the Linux distribution you're using. You can refer to the official Node.js documentation for detailed instructions.

After installing Node.js, run the node -v command again to verify that it's installed correctly.

Solution 2: Update PATH Configuration

If Node.js is already installed on your system, the next step is to check the PATH configuration. The PATH environment variable tells your system where to find executables. Follow these steps to update the PATH configuration:

  1. Windows:

    • Open the Start menu and search for "Environment Variables".
    • Click on "Edit the system environment variables".
    • In the System Properties window, click on the "Environment Variables" button.
    • In the "System variables" section, select the "Path" variable and click on the "Edit" button.
    • Add the path to the Node.js executable (e.g., C:\Program Files\nodejs) to the list of paths. Make sure to separate each path with a semicolon (;).
    • Click "OK" to save the changes.
  2. macOS and Linux:

    • Open your terminal and run the following command to open the .bashrc or .bash_profile file:

      shell
      nano ~/.bashrc

      or

      shell
      nano ~/.bash_profile
    • Add the following line at the end of the file:

      shell
      export PATH="/usr/local/bin:$PATH"
    • Press Ctrl + X to exit, then press Y to save the changes.

  3. Restart your terminal to apply the changes to the PATH configuration.

After updating the PATH configuration, run the node -v command again to check if the "node: command not found" error is resolved.

Solution 3: Reinstall Node.js

If the previous solutions didn't fix the issue, it's possible that your Node.js installation is corrupted. Reinstalling Node.js can help resolve this problem. Follow these steps to reinstall Node.js:

  1. Windows:

    • Uninstall Node.js from your system by going to "Control Panel" > "Programs" > "Uninstall a program". Select Node.js and click on the "Uninstall" button.
    • Download the latest version of Node.js from the official website.
    • Run the installer and follow the on-screen instructions to complete the installation.
  2. macOS:

    • Open your terminal and run the following command to uninstall Node.js using Homebrew:

      shell
      brew uninstall node
    • Download the latest version of Node.js from the official website.

    • Run the installer and follow the on-screen instructions to complete the installation.

  3. Linux:

    • The process of uninstalling and reinstalling Node.js on Linux depends on the package manager you're using. Here's an example using apt:

      shell
      sudo apt remove nodejs
      shell
      sudo apt install nodejs
    • Refer to the documentation of your package manager for specific instructions.

After reinstalling Node.js, run the node -v command to verify that the error is resolved.

Conclusion

The "node: command not found" error can be frustrating, but with the solutions provided in this tutorial, you should be able to fix it quickly. Remember to check your Node.js installation, update the PATH configuration, and reinstall Node.js if necessary. By following these steps, you'll be back to running Node.js commands in no time.

Remember, troubleshooting is a common part of a programmer's life, and it's important to stay calm and persistent when facing errors. Happy coding!

You can search for other posts at home page.
Minh Vu

Minh Vu

Software Engineer

Hi guys, I'm the author of WiseCode Blog. I mainly work with the Elastic Stack and build AI & Python projects. I also love writing technical articles, hope you guys have good experience reading my blog!