Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zshrc "command not found: npm"

So I'm looking into learning node.js. I've been following this tutorial to install it and try some stuff out.

At any rate, npm is behaving really strangely. It'll work just fine for a while (I changed its viewer config settings, for instance), but then suddenly 'disappear', so that the next time I enter

npm ...(anything)

in the command line, my shell informs me that the npm command has not been found. Previously, uninstalling and then reinstalling npm using nvm has worked, but I'd rather not do this every ten minutes, and it's super weird to me that it will suddenly stop working.

I think the last time, it stopped working after I edited and then re-sourced my ~/.zshrc file -- just added npm and node tab-completion plugins.

Any idea what's going on? I'll post the results of a couple basic commands, but I'm really bad with sysadmin stuff, and I don't really know what to look for here.

nvm -ls

   v0.8.22
current:    v0.10.2
default -> 0.8 (-> v0.8.22)

which node

/usr/bin/node

which npm

npm not found

There's are both nvm (is this a duplicate?) and npm directories directly in my root folder.

If I go into /usr/bin, node is a file, not a directory. There's also a file npm, but not file nvm.

As all the above probably shows, I really have no idea what's going on. I've searched for this, but nobody seems to have this problem. Any ideas what's going on or how to find out?

EDIT Noticing that my default node isn't up-to-date, I did all this. Putting aside why my shell switched me automatically to the bin directory (and why that seems to be a github repo -- no idea what's up with my computer), does this provide any clues?:

➜  node  nvm alias default v0.10.2
default -> v0.10.2
➜  node  nvm ls
   v0.8.22
current:    v0.10.2
default -> v0.10.2
➜  node  npm
zsh: command not found: npm
➜  node  nvm install npm
######################################################################## 100.0%
Checksums do not match.
Binary download failed, trying source.
Additional options while compiling: 
nvm: install N/A failed!
➜  bin git:(master) nvm uninstall npm
N/A version is not installed yet... installing
######################################################################## 100.0%
Checksums do not match.
Binary download failed, trying source.
Additional options while compiling: 
nvm: install N/A failed!
➜  bin git:(master) cd ~/rails/node

EDIT 2 - I made sure the node path was added to my zshrc file:

#other stuff...
export PATH=(...):/usr/bin/node

But when I then tried to run

source ~./zshrc

I got this complaint:

.zshrc:11: command not found: npm

which points to these lines (numbers added):

10 . ~/nvm/nvm.sh
11 . <(npm completion)

which I got from following the above tut and, back when npm was working, looking up how to implement tab completion.

In response to Jens

This is in my ~/nvm/nvm.sh file, near the start:

# Auto detect the NVM_DIR
if [ ! -d "$NVM_DIR" ]; then
    export NVM_DIR=$(cd $(dirname ${BASH_SOURCE[0]:-$0}) && pwd)
fi

# Make zsh glob matching behave same as bash
# This fixes the "zsh: no matches found" errors
if [ ! -z "$(which unsetopt 2>/dev/null)" ]; then
    unsetopt nomatch 2>/dev/null
fi

I tried to (and I think successfully did) chmod the npm directory to make it read/write/executable.

like image 620
Sasha Avatar asked Apr 03 '13 02:04

Sasha


9 Answers

So I was having the exact problem (I am also a student learning from zero), I am using Ubuntu 21.04 and after re installing VSC and updating OhMyZSH. Node still exists because when I do node -v I was able to watch the current version, but with npm -v it gave: zsh: command not found: npm

I was checking previous answers and trying to copy paste "commands" at writing this into the Terminal: ~ sudo nano ~/.zshrc , nothing works until I start checking the hidden files into the "Home" folder, and I notice that NODE was being "charged" in the LTS version, but NPM was in the Latest version, so i just change the PATH to this last one folder, and now it works again. Final result is like this for me: export PATH="$PATH:/home/myLaptopName/.nvm/versions/node/v17.0.1/bin" So my suggestion is to check if you already have Node with: node -v to know the version, also you can find the directory path with : whereis node and then start making some directories archaeology, until find npm file.

like image 177
Walter Alvarado Avatar answered Oct 08 '22 17:10

Walter Alvarado


1) Open your zsh config file

nano ~/.zshrc

2) Add path

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

3) Save the file by pressing CTRL + X and then Y

4) Executes the content of the file

source ~/.zshrc

This simple process worked for me :-)

like image 31
zarpio Avatar answered Oct 08 '22 16:10

zarpio


I had the issue where neither Node nor npm works. I spent about 6 hours searching for solution online, but no luck.

As npm website mention:

npm comes with node now.

I thought to install Node.js again, with a hope it will fix these issues, and it did :)

After installation was completely successful. Node installer shows this final message:

Node was installed at

/usr/local/bin/node

npm was installed at

/usr/local/bin/npm

Make sure that /usr/local/bin is in your $PATH.

And that's where I notice that my $PATH and installation directories were wrong. After installation I'm abel to run node and npm.

Hope it helps...

like image 43
egyamado Avatar answered Oct 08 '22 16:10

egyamado


Honestly, not sure if this will/would solve the problem you are/were having, but I was experiencing pretty much exactly what you detailed - where npm was working, but then randomly just completely stopped working - and I tried a ton of the same things people suggested in here with no luck.

What ended up doing the trick for me, as simple/dumb as this sounds/is, was:

source ~/.bash_profile

I wish I had a better explanation as to why that was the magic bullet, but at least it works!

Cheers!

like image 37
tklives Avatar answered Oct 08 '22 17:10

tklives


I had the same problem with my Mac, I did the following:

  1. Uninstall the existing node
  2. Go to the node webpage and download the package, then install it
  3. Go to ~/.zshrc and the path would usually be /usr/local/bin
  4. Have fun with npm
like image 21
Atul Kumar Avatar answered Oct 08 '22 15:10

Atul Kumar


Adding this to my ./zshrc file has helped me on ubuntu 19.10:

export NVM_DIR=~/.nvm [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"

Source: https://superuser.com/questions/1403007/zsh-npm-node-nvm-command-not-found-after-installing-ohmyzsh

like image 26
Ofelia Muradova Avatar answered Oct 08 '22 17:10

Ofelia Muradova


For me, and other googlers arriving here, the problem was that when calling sudo on Ubuntu, secure_path was being used, which didn't include NPM.

You can either disable secure_Path, which definitely doesn't seem recommended. So instead I just appended the node bin to secure path, when installed via NVM it looked like this for me

  • $: sudo su

  • $: visudo

Then adding /home/YourUsername/.nvm/versions/node/v8.5.0/bin: to the secure_path variable.

Further info: sudo changes PATH - why?

like image 42
nmu Avatar answered Oct 08 '22 16:10

nmu


curl -L https://npmjs.org/install.sh | sh

I tried it on a fresh Mac OS install and it works!

like image 36
DannyFeliz Avatar answered Oct 08 '22 16:10

DannyFeliz


I used the following command and that worked in ubuntu

export NVM_DIR=~/.nvm
 [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
like image 31
majid gabrlo Avatar answered Oct 08 '22 16:10

majid gabrlo