[Solved] “bash: node: command not found” Error

The “node: command not found” error in Bash indicates that the Node.js is either not installed or inaccessible. To fix this error, install Node.js using:

sudo apt install -y nodejs

The node command runs JavaScript code outside of a web browser. It is associated with Node.js, which is a runtime environment that allows developers to execute JavaScript code on the server side or command line. However, “bash: node: command not found” indicates either the Node.js is not installed on your system or the PATH variable is not set for node. Let’s see the reasons for this error in detail and explore the possible fixes.

Reasons for “node: command not found” Error in Bash

Some common reasons for the “node” command not found error are:

  • Node.js not Installed: If you cannot use the “node” command, it could be that your system does not have Node.js installed.
  • NVM not Installed: The absence of Node.js Version Manager (NVM) can be a reason for the node command not found error.
  • Incorrect Path: The PATH on your system may not contain “node”.
  • Inadequate Permissions: “node” might not have the authorization to function.
  • Shell Configuration Issues: Your shell configuration files (e.g. ~/.bashrc, ~/.bash_profile, ~/.zshrc) may not be properly configured to include the directory where Node.js is installed.
  • Multiple Node.js Versions: If you have multiple versions of Node.js installed, make sure the correct version is selected.

4 Methods to Solve “bash: node: command not found” Error

Follow the below methods to solve the node command not found error in Bash:

1. Install Node.js

Most of the time, the absence of Node.js on your system causes the “bash: node: command not found” error. In this instance, APT in Ubuntu, YUM in RedHat, and Pacman in Arch Linux can install it on your system. Follow the steps to install Node.js in Ubuntu:

  1. Press CTRL+ALT+T to open the terminal in Ubuntu.
  2. Update the repository using sudo apt update command.
  3. Run the command to install Node.js:
    sudo apt install -y nodejs

    installing Node.js in Ubuntu

  4. Execute node command to verify the installation.executing node commandNow, the “node” command is executed successfully.
Note: Press CTRL+Z to exit this prompt.

2. Set “node” PATH Variable

A prevalent reason behind the “node command not found” is it is not present in your system PATH environment variables. To set the PATH variable for “node”, follow the steps below:

  1. Press CTRL+ALT+T to open the terminal.
  2. Use which command to check the current path of node:
    which node

    executing which node command

  3. Open bashrc file using nano command:
    nano ~/.bashrc
  4. Now, paste export PATH=$PATH:<path to node> code replacing <path to node> with the which command’s output and paste it at the end of the file. For example:

    export PATH=$PATH:/usr/bin/node

    adding node path variable to bashrc file

  5. Press CTRL+S to save, and CTRL+X to exit.
  6. Then, execute the .bashrc file using:
    source ~/.bashrc
  7. Now you can check running a node command. For example:
    node --version

    executing bashrc file and checking node version

After adding the path, the node command ran without any error.

3. Set an Alias to “node”

To fix the “node: command not found” error, you can create a temporary solution by setting up an alias for node. Here’s how you can do it:

echo "alias node='$(which node)'" >> ~/.bashrc; . ~/.bashrc

This command appends an alias for the “node” command to the ~/.bashrc file and immediately applies the changes in the current terminal session. The alias ensures that the node command points to the correct path by using the $(which node) syntax, which dynamically retrieves the actual path to the node executable.

4. Create a Symbolic Link to “node”

A symbolic link in Linux is a special type of file that serves as a reference or pointer to another file or directory. It acts as a shortcut, allowing users to create a link to a file or directory without copying its content. To create a symbolic link to “node”, run the below command:

sudo ln -s $(which node) /usr/local/bin/node

This command will create a symbolic link called node in the /usr/local/bin directory. Then, you can run node command error-free.

How to Solve “node: command not found” Error in MacOS?

To solve “node: command not found” error in MacOS, follow the steps below:

  1. Open your Mac terminal and run the below command to install node:
    brew install node@8
  2. Link “node” to the system default directory using:
    brew link node@8
  3. Execute node --version to check the installation.
  4. If you still find the error, execute:
    brew link --overwrite --force node@8

This command overwrites any existing symbolic links related to Node.js and ensures that the specified version is linked and active on the system.

Conclusion

In conclusion, resolving the “bash: node: command not found” issue involves ensuring the installation of Node.js and configuring the system’s PATH appropriately. Applying these solutions allows you to seamlessly access node commands within the Bash environment and streamline various aspects of building server-side applications, APIs (Application Programming Interfaces), and tools in JavaScript.

People Also Ask

How to install node command?

To install the node command, you need to download and install Node.js. For that, visit the official Node.js website, download the appropriate installer for your operating system, and follow the installation instructions. In Linux, you can install node using sudo apt install nodejs.

How do I know if NodeJS is installed?

To check if Node.js is installed on your system, you can open a terminal or command prompt and run node -v command. If Node.js is installed, this command will display the installed Node.js version.

How to install node command line Mac?

To install Node.js and npm (Node Package Manager) on a Mac, follow the steps:

  1. Open Terminal and run the following command to install Homebrew:
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  2. Install Node.js and npm using:
    brew update
    brew install node
  3. Run node -v to verify the installation.

Why can’t I run a node command from a Bash script?

If you encounter difficulties running a Node.js command from a Bash script, potential issues may include problems with the script’s PATH, incorrect permissions, a missing or inaccurate shebang line, or version mismatches.

Related Articles


<< Go Back to [Solved] “bash: command not found” Error | Bash Error Handling and Debugging | Bash Scripting Tutorial

5/5 - (2 votes)
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
Ashikur Rahman

Hello, I’m Ashikur Rahman, currently working as a Linux Content Developer Executive at SOFTEKO. I have completed my graduation in Computer Science and Engineering from Khulna University of Engineering & Technology (KUET). Also, I’m pursuing my master’s in the same department at Bangladesh University of Engineering & Technology (BUET). I like to learn new technologies, contribute, and share those with others. Here my goal is to provide beneficial and user-friendly articles on Linux distribution for everyone. Read Full Bio

Leave a Comment