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:
- Press CTRL+ALT+T to open the terminal in Ubuntu.
- Update the repository using
sudo apt update
command. - Run the command to install Node.js:
sudo apt install -y nodejs
- Execute
node
command to verify the installation.Now, the “node” command is executed successfully.
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:
- Press CTRL+ALT+T to open the terminal.
- Use which command to check the current path of node:
which node
- Open bashrc file using nano command:
nano ~/.bashrc
-
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
- Press CTRL+S to save, and CTRL+X to exit.
- Then, execute the .bashrc file using:
source ~/.bashrc
- Now you can check running a node command. For example:
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:
- Open your Mac terminal and run the below command to install node:
brew install node@8
- Link “node” to the system default directory using:
brew link node@8
- Execute
node --version
to check the installation. - 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:
- Open Terminal and run the following command to install Homebrew:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Install Node.js and npm using:
brew update brew install node
- 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
- [Fixed] “bash: sudo: command not found” Error
- [Solved] Bash “yarn” Command Not Found Error
- [Solved] Bash “docker” Command Not Found Error
- [Fixed] “bash: ng: command not found” Error
- [Fixed] “bash: java: command not found” Error
- Solved “bash: yum: command not found” [RHEL, Fedora, CentOS]
- [Solved] “bash: vim: command not found” Error
- [Fixed] “bash: curl: command not found” Error
- [Solved] “bash: code: command not found” Error
- [Fixed!] “systemctl command not found” Error in Linux
<< Go Back to [Solved] “bash: command not found” Error | Bash Error Handling and Debugging | Bash Scripting Tutorial
FUNDAMENTALS A Complete Guide for Beginners