How to Solve NPM Command Not Found Error in Bash

Yahya Irmak Mar 14, 2025 Bash Bash Error
  1. Understanding the npm Command Not Found Error
  2. Method 1: Installing Node.js and NPM
  3. Method 2: Adding NPM to Your System PATH
  4. Method 3: Fixing Permission Issues
  5. Conclusion
  6. FAQ
How to Solve NPM Command Not Found Error in Bash

If you’re a developer, you’ve probably encountered the dreaded “npm command not found” error at some point while working in a Linux Bash environment. This frustrating message often appears when you try to run npm commands, preventing you from managing your Node.js packages effectively. Whether you’re just getting started with Node.js or are a seasoned developer, understanding how to resolve this issue is crucial for maintaining a smooth workflow.

In this article, we will explore the common causes of the npm command not found error and provide step-by-step solutions to help you get back on track. Let’s dive in!

Understanding the npm Command Not Found Error

Before we jump into the solutions, it’s essential to understand why this error occurs. The “npm command not found” error typically arises when your system cannot locate the npm executable. This can happen for several reasons, including:

  • Node.js and npm are not installed.
  • The npm executable is not in your system’s PATH.
  • There are permission issues preventing npm from running.

Identifying the root cause is the first step in resolving the issue.

Method 1: Installing Node.js and NPM

The most straightforward solution to the npm command not found error is to ensure that Node.js and npm are installed on your system. Node.js comes with npm bundled, so installing Node.js automatically installs npm as well.

To install Node.js and npm on your Linux system, you can use the following commands:

sudo apt update
sudo apt install nodejs npm

After running these commands, you can verify the installation by checking the versions of Node.js and npm:

node -v
npm -v

Output:

v14.17.6
6.14.15

If you see version numbers, congratulations! Node.js and npm are installed correctly. If you still encounter the npm command not found error, the issue may lie elsewhere.

Installing Node.js and npm ensures that you have the necessary tools to manage your JavaScript packages. If they were not installed previously, this method will resolve the error effectively. However, if they were already installed, you might need to check your system’s PATH.

Method 2: Adding NPM to Your System PATH

If Node.js and npm are installed but you still receive the npm command not found error, the next step is to check if the npm executable is included in your system’s PATH. The PATH is an environment variable that tells your system where to look for executable files.

To check if npm is in your PATH, run the following command:

echo $PATH

This command will display the directories included in your PATH. You need to ensure that the directory containing the npm executable is listed. Typically, npm is installed in /usr/bin/ or /usr/local/bin/. If you don’t see the correct directory, you can add it to your PATH by editing your shell configuration file (like .bashrc or .bash_profile).

To add npm to your PATH, use the following command:

echo 'export PATH=$PATH:/usr/local/bin' >> ~/.bashrc
source ~/.bashrc

Output:

<No output>

After executing these commands, the changes will take effect. You can verify by running the npm command again:

npm -v

Output:

6.14.15

If the version number appears, you’ve successfully added npm to your PATH. This method is crucial because even if npm is installed, if it’s not in your PATH, your system won’t recognize the command.

Method 3: Fixing Permission Issues

Sometimes, the npm command not found error can be caused by permission issues. If you installed Node.js and npm using a package manager, there might be restrictions on executing npm commands. This is common in environments where multiple users share the same system.

To fix permission issues, you can try reinstalling npm with the following command:

sudo npm install -g npm

This command installs npm globally with the necessary permissions. After installation, check the npm version again:

npm -v

Output:

6.14.15

If you still face issues, consider changing the ownership of the npm directory. You can do this with the following command:

sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}

Output:

<No output>

By changing the ownership, you ensure that you have the necessary permissions to execute npm commands without any roadblocks. This method is particularly useful for users who frequently encounter permission-related errors.

Conclusion

Encountering the npm command not found error in Bash can be a frustrating experience, but it’s usually straightforward to resolve. By ensuring that Node.js and npm are installed, adding npm to your system PATH, and addressing any permission issues, you can quickly get back to developing your projects. Remember, keeping your development environment configured correctly is key to a smooth workflow. With these solutions at your disposal, you can tackle any npm-related issues with confidence.

FAQ

  1. What should I do if npm is still not found after installation?
    Check your system’s PATH to ensure the npm executable is included.

  2. Can I use npm without installing Node.js?
    No, npm is a package manager that requires Node.js to function.

  3. How do I check if Node.js is installed on my system?
    Run the command node -v to check if Node.js is installed.

  4. Is it safe to use sudo with npm install?
    It’s generally not recommended to use sudo with npm install due to potential permission issues.

  5. What is the difference between npm and Node.js?
    Node.js is a runtime environment for executing JavaScript, while npm is a package manager for managing libraries and dependencies.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
Author: Yahya Irmak
Yahya Irmak avatar Yahya Irmak avatar

Yahya Irmak has experience in full stack technologies such as Java, Spring Boot, JavaScript, CSS, HTML.

LinkedIn

Related Article - Bash Error