zshrc "command not found: npm"
Asked Answered
P

10

14

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.

Pongee answered 3/4, 2013 at 2:3 Comment(5)
Does your PATH point to where node is installed?Restaurant
@Restaurant -- tried to do that, but I'm not 100% sure where that is. I added /npm to the path, cause of that npm folder in root. And node.js is otherwise working just fine, so I figure THAT is correctly added to the path. How do I find out what directories to add to my path? And Jessemon -- how did you fix that?Pongee
Yes, it does, now, at least. But now that I added that, just trying to load the zshrc file now grumbles ".zshrc:11: command not found: npm", pointing to the line where I added the nvm path a while ago (". ~/nvm/nvm.sh")Pongee
Then by all means use /full/path/to/npm completion.Recollection
I used the full path as per @Jens. I think I was also using sudo which may have caused the problem to begin with.Genni
P
8

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 :-)

Pressroom answered 1/4, 2016 at 23:5 Comment(0)
P
7

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!

Peripatetic answered 26/10, 2016 at 17:51 Comment(0)
O
6

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...

Ownership answered 7/4, 2015 at 1:43 Comment(0)
D
2

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
Diversified answered 3/11, 2016 at 9:57 Comment(0)
C
2

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

Chaoan answered 17/4, 2020 at 16:23 Comment(0)
M
2

I used the following command and that worked in ubuntu

export NVM_DIR=~/.nvm
 [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
Malonis answered 27/8, 2021 at 13:16 Comment(2)
This is the same solution as in this other answer.Unyoke
Please provide additional details in your answer. As it's currently written, it's hard to understand your solution.Natika
A
1
curl -L https://npmjs.org/install.sh | sh

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

Ause answered 3/1, 2017 at 14:28 Comment(1)
This work for me, my problem was for the symbolic link... ty!August
L
1

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?

Liebfraumilch answered 15/9, 2017 at 10:33 Comment(0)
V
1

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.

Vorfeld answered 25/10, 2021 at 17:35 Comment(0)
H
0

Try to install the node and npm again, through Nodesource. Follow these steps:

  1. Enable the NodeSource repository by running the following curl command as a user with sudo privileges :

    curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -

The command will add the NodeSource signing key to your system, create an apt sources repository file, install all necessary packages and refresh the apt cache.

If you need to install another version, for example 14.x, just change setup_12.x with setup_14.x

  1. Once the NodeSource repository is enabled, install Node.js and npm by typing:

    sudo apt install nodejs

The nodejs package contains both the node and npm binaries.

Verify that the Node.js and npm were successfully installed by printing their versions:

node --version and npm --version

Reference: https://linuxize.com/post/how-to-install-node-js-on-ubuntu-18.04/

Hardtop answered 19/7, 2020 at 23:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.