Raphaël
Raphaël

Reputation: 2051

NPM not found when using NVM

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 ?

Upvotes: 68

Views: 112129

Answers (23)

JeffryHouser
JeffryHouser

Reputation: 39408

Just one more answer in case this helps someone.

I am using NVM on Windows. Everything was working fine on command line. I set up a pre-push hook using Husky, and started to get this error from Github Desktop:

/bin/bash: C:/Program Files/nodejs/npm: No such file or directory with nvm installed

There is an npm file there, which seems to be a script. npm is actually in "C:/Program Files/nodejs/node_modules"

I ended up switching to Source Tree for pushing to my repo, which is not running into this problem.

Upvotes: 0

Aissam
Aissam

Reputation: 1

1. Verify If the NVM_HOME and NVM_SYMLINK environment variables are set

  1. Make sure that C:\Program Files\nodejs is set in the Path Env.

Upvotes: 0

Shi Kin Ma
Shi Kin Ma

Reputation: 73

For windows users, nvm for Windows is a little tricky.

Check 1. The node path is actually in the Environment variables. in my case, it is ~\AppData\Roaming\npm The node path is necessary

Check 2. The %NVM_HOME% and also %NVM_SYMLINK% is set. And they also should be set in the PATH variable

See if they are properly set

Check 3. The npm path actually contains the node.js. If it doesn't, it probably is a reminant of the old installation. In my case, I remove it with rm -rf. The reason it blocks nvm, is that nvm(fow windows) have to set up sym link in that path. If there is a real file in the path, nvm(for windows) will fail silently. Original NPM package

The above image is missing the node. instead it should be like:

Proper nvm synlink

Even if the symlinks doesn't work, and the environment variables are wrong, It should be possible to run node and npm from this directory directly.

Upvotes: 0

Nurullah Yayan
Nurullah Yayan

Reputation: 1

Windows Answer : Open Powershell to Administration.

"Set-ExecutionPolicy -ExecutionPolicy RemoteSigned" write and Enter After Y or A to problem fixed.

Upvotes: 0

Tuychiev Toir
Tuychiev Toir

Reputation: 380

I use Mac air and pro. I always strugled with this problem a lot. After appending this line to ~/.zshrc file (or your profile file). It worked correctly, so feel free to try ;)

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[[ -r $NVM_DIR/bash_completion ]] && \. $NVM_DIR/bash_completion

Upvotes: 0

TTM
TTM

Reputation: 56

It was as simple as

nvm use <desired node version>

Thanks to zhuhang.jasper for the inspiration.

Upvotes: 3

Matteus Barbosa
Matteus Barbosa

Reputation: 2743

For me, it only worked in Windows 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.

Upvotes: 4

lucamonte86
lucamonte86

Reputation: 1

For all those who are still trying to install NVM on Windows and all the tips here have not helped: try the following (this was the only thing that worked for me).

I think the main problem is that probably most people here were already using Nodejs before installing NVM - therefore there's most likely an old NodeJS folder under C:\Program Files\nodejs.

Now try to uninstall NVM and Nodejs, and do not forget to remove the folder C:\Program Files\nodejs manually (!!!). Then reinstall NVM with administrator rights (I used the compiled installer from NVM - repository).

Now do these steps:

  1. Open a new command line CMD.exe
  2. Try to verify, if nvm is installed by typing: nvm --version
  3. And then go ahead with: nvm install latest
  4. nvm use [here comes your version]
  5. nvm on

This finally (!!!) worked for me!

Upvotes: 0

Stefan
Stefan

Reputation: 12380

A. Also see troubleshooting section at nvm documentation:

https://github.com/nvm-sh/nvm#troubleshooting-on-linux

Troubleshooting on Linux On Linux, after running the install script, if you get nvm: command not found or see no feedback from your terminal after you type command -v nvm, simply close your current terminal, open a new terminal, and try verifying again. Alternatively, you can run the following commands for the different shells on the command line:

bash: source ~/.bashrc

zsh: source ~/.zshrc

ksh: . ~/.profile

These should pick up the nvm command.

B. If you want to use the npm command in a Docker file, consider to append it to the nvm command, for example

ENV NVM_DIR /root/.nvm
ENV NODE_VERSION 18.17.0 # 20.10.0 is not supported by geneator-office, yet

RUN curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.39.7/install.sh | bash \
  && . $NVM_DIR/nvm.sh \ 
  && nvm install $NODE_VERSION \ 
  && nvm alias default $NODE_VERSION \
  && nvm use default \
  && npm install -g npm \
  && npm install -g yo generator-office 

instead of

RUN curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.39.7/install.sh | bash \
  && . $NVM_DIR/nvm.sh \ 
  && nvm install $NODE_VERSION \ 
  && nvm alias default $NODE_VERSION \
  && nvm use default \

RUN npm install -g npm 
RUN npm install -g yo generator-office 

Upvotes: 0

Casimiro Rocha
Casimiro Rocha

Reputation: 41

In my case the problem was that when installing NVM I did not define the default version, check if this is your case by running the command:

nvm list

If the data: "default" is similar to "default -> lts (-> N/A)"

You have the same problem as I had, let's solve it by doing the following:

After installing nvm, run the command: "nvm install node --lts" without the quotes!

If everything goes well, it will display something like:

Installing latest LTS version. Now using node v18.18.0 (npm v10.1.0)

In my case, note that the exact installed version of lts was "v18.18.0" you need to set a default version otherwise, running the nvm list command you will see that the nvm default will be set to N/A

If after running the "nvm list" command you notice that the "default" parameter is:

default -> lts (-> N/A)

It means that the default version for use has not been defined, which is why you can run nvm in the terminal, but you cannot run npm or node -v

So finally, resolve this by setting the default version for NVM:

Run: "nvm list" and see if it appears in the list: "lts/hydrogen -> v18.18.0"

Now run: "nvm alias default lts/hydrogen" If ok, it will display the message: "default -> lts/hydrogen (-> v18.18.0)"

Finish with the command: "nvm use default" If ok, it will display the message: "Now using node v18.18.0 (npm v10.1.0)"

Checking before and after running the "nvm list" command

Before: default -> lts (-> N/A)

After: default -> lts/hydrogen (-> v18.18.0)

Hope this helps!

Upvotes: 4

xhafan
xhafan

Reputation: 2416

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.

Upvotes: 0

Alexander P
Alexander P

Reputation: 31

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.

Upvotes: 2

unicorn
unicorn

Reputation: 119

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.

Upvotes: 8

Stephen
Stephen

Reputation: 31

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/

Upvotes: 1

zhuhang.jasper
zhuhang.jasper

Reputation: 4475

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

Upvotes: 4

TheMechanic
TheMechanic

Reputation: 957

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

Upvotes: 8

rflw
rflw

Reputation: 1785

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.

Upvotes: 0

hlopezvg
hlopezvg

Reputation: 605

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

Upvotes: 1

NeoZoom.lua
NeoZoom.lua

Reputation: 2921

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

Upvotes: 66

Rapha&#235;l
Rapha&#235;l

Reputation: 2051

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.

Upvotes: 57

Victor Jatob&#225;
Victor Jatob&#225;

Reputation: 837

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

Upvotes: -3

Vaibhav Jain
Vaibhav Jain

Reputation: 2417

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

Upvotes: 7

Heartbit
Heartbit

Reputation: 1836

I fixed this by doing this command:

$ command -v npm

and then reopen the shell window.

Upvotes: 21

Related Questions