2

The Node version is 17.7.1. When I run npm -v I get:

bash: /usr/local/bin/npm: No such file or directory

But when I run npm root -g I get:

/usr/local/lib/node_modules

Running npm list -g also gives me the same message as running npm -v.


So, where is it supposed to be and how can I fix this?

0

2 Answers 2

4
+50

There are a few issues as to why this is occuring.


It appears that your computer cannot find the following path (below) to the npm folder, but it can find the global node_modules folder.

/usr/local/bin/npm

This may occur for a number of reasons. Luckily, there are many solutions to these issues.


1. Reinstall Node.js and npm^

The first solution is pretty obvious, and it is to reinstall Node.js and npm^.

The installation process is very simple.

  1. If you are using only the command line, you can install Node.js using the following command. The npm command comes installed with Node.js.

    $ sudo apt-get install nodejs
    

    Or, if you want to use the installer, download and run the installer from nodejs.org.

  2. Check the versions of Node.js and npm to see if it has worked.

    $ node -v
    $ npm -v
    

The helpful thing about reinstalling Node.js and npm in this case is that, if Node.js and npm weren't installed in the first place (highly unlikely), it will install them.

However, if they were already installed, but then something happened to the installation directory (e.g. it was renamed, moved, deleted or corrupted), then the new installation will most likely have fixed that issue.


2. Add /usr/local/bin/npm to the path (solution worked for OP)

If the first solution didn't work for you, then the next issue that might be occurring might be that /usr/local/bin/npm isn't in your path, which means that bash cannot find it.

To add npm to your path, follow the steps below.

  1. Normally, to get the path with npm, you need to run the following command.

    $ npm bin
    

    Obviously, we have to use a workaround. But, we can just use an assumed path for where the binaries of npm are stored, which is...

    /usr/local/bin/npm/bin
    

    However, this may vary, so, to be sure that this exists, you can simply type it into the command line.

    $ cd /usr/local/bin/npm
    

    Then, you can run the ls command, and see if there are any bin folders or .sh shell scripts that might be used for the path.

    $ ls
    

    Make sure you save the binary part for later!

  2. Open the home directory using the following command. This is because the .bashrc file is in the home directory (used in the next step).

    $ cd $HOME
    
  3. Open the .bashrc file using vi (or any file editor of your choice, but vi will be used in this example).

    $ vi .bashrc
    

    Then, press the i key for Insert Mode. Type the following in the file.

    export PATH=/usr/local/bin/npm/bin:$PATH
    

    In vi, press Esc, then press the following keys: :, w, q, !, Enter.

    :wq!
    

    The file is now saved and the editor is closed.

  4. Normally, the .bashrc file is only read when you log in each time. To avoid doing that, you can force bash to read the .bashrc file and update itself. To do this, run the following command.

    $ source .bashrc
    

    The bash shell should now be restarted.

These were quite a few steps (four, but many were compressed into one), but if a situation occurred where for some reason npm wasn't adding itself to the path, this solution should fix it.


3. Denied read/write access

Another issue is that your user profile doesn't have the right permissions to read/write to usr/bin/local/npm, or you didn't give npm permissions.

The fix to this solution is very simple.

  1. Run the npm commands you want to run using sudo.

    $ sudo npm -v
    $ sudo npm list -g
    
  2. If you want, you can make the commands you run automatically run with sudo.

    $ sudo -s
    $ npm -v
    $ npm list -g
    

Warning: If you don't have the password for sudo, you can't run any of the commands using sudo above.

This should fix your issue, if denied read/write access is the problem.


There are most likely many more solutions^^, but they might be added later.


  • ^ This is a very usual process, but it has to be added here so that I can be sure that this solution has been tried.

  • ^^ These solutions may be found later and edited to this answer. Edited once.

2
  • thank you. I had also other problems after installing some node version managers (suggested by someone) and point 2 of your answer helped me solve them
    – Tinaira
    Commented May 1, 2022 at 13:21
  • Glad the answer helped! I've edited it to show which one worked for you.
    – Viradex
    Commented May 1, 2022 at 22:26
0

use this command :

npm list -g

you should see your npm directory at first line

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.