60

I have installed node/npm using the nvm documentation.

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.29.0/install.sh | bash

Then:

nvm install node

At this point node is working but the npm command result with:

npm: command not found

How can I have npm to work correctly ?

Raphaël
  • 1,924
  • 2
  • 18
  • 22

14 Answers14

53

I found out that this was a conflict with a previous versions of npm that have not been removed properly despite a apt-get remove node.

I solved it by reinstalling npm from scratch:

rm -R ~/.npm ~/.nvm
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
nvm install node

I found the solution here.

Niko9911
  • 361
  • 1
  • 6
  • 13
Raphaël
  • 1,924
  • 2
  • 18
  • 22
  • 10
    Just a note to anyone copying and pasting the whole string: you'll probably want to use [a newer version of the nvm install script](https://github.com/creationix/nvm#installation). – ropeladder Dec 16 '17 at 16:52
  • Updated the link. However, like said, latest can be always found from the repository. – Niko9911 Sep 19 '19 at 06:23
  • 1
    Didn't work for me--I never had nodejs, npm, or nvm installed, tried with this anyway: `apt-get update --quiet && apt-get purge -y nodejs npm && apt-get autoremove -y && rm -rf ~/.npm ~/.nvm && apt-get clean` – Val Kornea Nov 05 '19 at 18:31
35

If you run NVM-Windows, don't forget to run nvm on. (this solve the problem as title for me.)

NeoZoom.lua
  • 2,269
  • 4
  • 30
  • 64
19

I fixed this by doing this command:

$ command -v npm

and then reopen the shell window.

Heartbit
  • 1,698
  • 2
  • 14
  • 26
  • For me, this worked and not the accepted answer as I had no installations of npm previously. – Akash Jobanputra Sep 03 '18 at 05:51
  • I think this allow npm to get in your shell again after installing nvm... for more details read this question -> https://askubuntu.com/questions/512770/what-is-use-of-command-command – Heartbit Jun 01 '21 at 10:47
7

I solved it by uninstalling all problematic node versions (e.g. v14 below) and reinstalling it.

The problem:

node --version; npm --version;
v14.17.1
Command 'npm' not found, but can be installed with:
sudo apt install npm

The solution:

nvm deactivate

echo "All versions BEFORE:"
nvm_ls

# uninstall all 14.* versions
for v in $(nvm_ls 14); do nvm uninstall $v; done

echo "All versions AFTER:"
nvm_ls

# reinstall version 14
nvm install 14

# and now it has npm too
node --version; npm --version
which node; which npm

# v14.17.1
# 6.14.13
# /home/user/.nvm/versions/node/v14.17.1/bin/node
# /home/user/.nvm/versions/node/v14.17.1/bin/npm

TheMechanic
  • 820
  • 1
  • 8
  • 23
7

If you use Windows OS, make sure you removed the existing nodejs and npm. In my case, it worked well after I remove the C:/Program Files/nodejs. Reference is here.

During nvm installation, make sure the selected path must NOT exist.

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
unicorn
  • 111
  • 1
  • 3
6

This problem especially happens in windows which happens because of missing admin rights for cmd.

If you are using Git bash

  1. Go in installation directory e.g C:\Program Files\Git
  2. Right click properties -> compatibility.
  3. Tick the checkbox with label -> Run as administrator.
  4. Run the git bash again & execute npm list and then npm use 'version_to_be_used'

Same goes for Cmd

Vaibhav Jain
  • 1,939
  • 26
  • 36
3

One possible reason is the NVM symlink is invalid.

  1. But first, check if both NVM_HOME & NVM_SYMLINK is already set in environment path.
    If not, maybe some problem with your nvm installation and u might want to reinstall.
  2. Using explorer, open the symlink folder to check if the folder is valid.
    Default Symlink path in Windows: C:\Program Files\nodejs. Symlink appears as a normal shortcut in Windows explorer.
    If you see node files in there, then you're fine.
    If the folder is invalid, delete the symlink.
    Then, execute nvm ls and nvm use <desired node version>, this step will re-create the correct symlink.
  3. Restart CMD and test nvm current, node -v, npm -v
zhuhang.jasper
  • 4,120
  • 4
  • 21
  • 29
2

For Windows:

nvm creating symlink from installed node path like c:\program files\node to the c:\users<your user>\AppData\nvm<node ver>

So check:

  1. Your basic node path in the PATH variable.
  2. Your npm is inside c:\users<your user>\AppData\nvm<node ver>\nmp and this path is also int the PATH variable.
Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
1

You could also run

source ~/.bashrc

and try to run again on the same bash terminal where you downloaded the install.sh the command:

npm -v

hlopezvg
  • 555
  • 5
  • 11
1

I went through a similar issue recently and solved it by setting the npm mirror to npm_mirror https://github.com/npm/cli/archive/refs/tags/

The default npm mirror (https://github.com/npm/cli/archive) was a broken link.

so run

nvm npm_mirror https://github.com/npm/cli/archive/refs/tags/
Stephen
  • 21
  • 1
  • 2
0

I had the same issue while any new terminal instance started up the message 'npm not found' was shown. I noticed that I had defined (probably) custom paths to npm and node in ~/.bashrc. Deleting them (keeping the paths for nvm) resolved the problem.

rflw
  • 1,747
  • 17
  • 26
0

This helped me: https://github.com/coreybutler/nvm-windows/issues/548#issuecomment-768297716 Adding quotes to NVM_SYMLINK environment variable: "C:\Program Files\nodejs" instead of C:\Program Files\nodejs.

xhafan
  • 2,140
  • 1
  • 26
  • 26
0

For me, it only worked after

  1. reinstalling nvm through the .exe package, and
  2. reinstalling node version in a way that Windows is able to add everything properly to environment variables
  • nvm uninstall < nodeversion >
  • nvm install < nodeversion >
  • nvm use < node version >
  • npm -v should display your npm version

Remember to run everything in a prompt as administrator to avoid symlink permission issues.

Matteus Barbosa
  • 2,409
  • 20
  • 21
-3

Install node using node source distribution:

curl -sL https://deb.nodesource.com/setup_[version].x | bash -
apt-get install -y nodejs

[version] = the wanted version. See the repository to choose the correct: NodeSource Node.js Binary Distributions

Victor Jatobá
  • 811
  • 7
  • 13
  • nvm also installs node using the source distribution. However, nvm retains node versions so you can quickly upgrade and re-install all global modules. – TamusJRoyce Aug 23 '19 at 02:45
  • 1
    That's not what the question asks. It would be kind to not to suggest to just use something else as an answer since it doesn't answer to OP's question, unless necessary. OP Clearly uses NVM for a reason (eg. can switch between node versions). – Eksapsy Aug 24 '19 at 14:43