Command not found after npm install in zsh
Asked Answered
B

32

171

I'm having some problems installing vows via npm in zsh. Here's what I get. I tried installing it with and without the -g option. Do you have any idea what's wrong here?

[❤  ~/Desktop/sauce-node-demo:master] npm install -g vows
npm http GET https://registry.npmjs.org/vows
npm http 304 https://registry.npmjs.org/vows
npm http GET https://registry.npmjs.org/eyes
npm http GET https://registry.npmjs.org/diff
npm http 304 https://registry.npmjs.org/eyes
npm http 304 https://registry.npmjs.org/diff
/usr/local/share/npm/bin/vows -> /usr/local/share/npm/lib/node_modules/vows/bin/vows
[email protected] /usr/local/share/npm/lib/node_modules/vows
├── [email protected]
└── [email protected]
[❤  ~/Desktop/sauce-node-demo:master] vows
zsh: command not found: vows
Boles answered 5/10, 2012 at 10:2 Comment(3)
Have you tried refreshing the shell's path cache via rehash ?Corinnecorinth
still nothing after doing this =/Boles
I don't know why, but going to bash (by typing bash itself and hit enter) in the same terminal then switching back to zsh (by typing zsh itself and hit enter), fixes it!!Thirtyeight
B
125

If you installed Node.js using Homebrew, npm binaries can be found in /usr/local/share/npm/bin.

You should make sure this directory is in your PATH environment variable. So, in your ~/.zshrc file add

export PATH=/usr/local/share/npm/bin:$PATH
Baxy answered 28/3, 2013 at 21:42 Comment(3)
On mac, I added export PATH="$HOME/.npm-packages/bin:$PATH" to my ~/.zshrc.Bungle
$HOME/.npm-global/bin worked for me with npm v6.9.0Coxswain
What if you didn't install Node.js via Homebrew or you can't remember? And how do you find 'usr/local/share/npm/bin'?Catheterize
C
210

add source /home/YOUUSERNAME/.bash_profile at the beginning of ~/.zshrc

And all missing commands will be detected.

For Mac users : add source /Users/YOUUSERNAME/.bash_profile

Covin answered 2/11, 2016 at 8:37 Comment(5)
For me I had to source ~/.bashrcRoseannroseanna
I'm using WSL on Windows 10, and I had to add `source /home/USERNAME/.profile' to get it to work, but it worked like a charmFauna
I had to source .bashrc instead of bash_profile. When I sourced bash_profile I got a ton of commands not found in zshBusra
My user folder in Mac OS Catalina does not contain this .bash_profile. I cant find it with search either. After updating the .zshrc file and reopening the terminal, its first line was no such file or directory: /Users/m...i/.bash_profile. Might this be a breaking change of Catalina?Informative
This also works for Mac users: source /Users/$USER/.bash_profile or maybe even easier source $HOME/.bash_profileDrub
B
125

If you installed Node.js using Homebrew, npm binaries can be found in /usr/local/share/npm/bin.

You should make sure this directory is in your PATH environment variable. So, in your ~/.zshrc file add

export PATH=/usr/local/share/npm/bin:$PATH
Baxy answered 28/3, 2013 at 21:42 Comment(3)
On mac, I added export PATH="$HOME/.npm-packages/bin:$PATH" to my ~/.zshrc.Bungle
$HOME/.npm-global/bin worked for me with npm v6.9.0Coxswain
What if you didn't install Node.js via Homebrew or you can't remember? And how do you find 'usr/local/share/npm/bin'?Catheterize
L
73

If you have added using nvm please add the following to your .zshrc file and restart the terminal since the binaries of the file are not being detected by zsh shell we specify the path

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
Lazarolazaruk answered 12/3, 2020 at 6:59 Comment(0)
M
35

Mac users only
assuming you installed nvm prior, and npm correctly
(step-by-step guide below on how to install it:
install nvm for Mac users ).

you need to:

Find the '.zshrc' file:

  • Open Terminal.
  • Type open ~ to access your home directory.
  • Press Cmd + Shift + . to show the hidden files in Finder.
  • Locate the .zshrc.

Edit the '.zshrc' file:

  • add: source /Users/_user_Name_/.bash_profile to the top of the file (where _user_Name_ stands for your user.

  • Save the file, and close the Terminal window.

Morman answered 18/10, 2019 at 21:33 Comment(5)
Even though this may look like a hack, I find it to be a good way to bring in all of my pre-installed stuff from bash to a zsh after Catalina upgrade.Pictograph
This, for me, is the answer, as simply running source /Users/_user_Name_/.bash_profile lasted only for the duration of the current session.Imbalance
Just so it's clear, it looks like 'open ~' will open a new Finder window, which you could just open normally. And 'Cmd + Shift + .' is a toggle to hide/show hidden files. I believe the ability to view hidden files from Finder is a setting that has to be configured first, so maybe going through Terminal is the way around that. But if this setting has already been configured, it doesn't seem necessary to use Terminal to open Finder.Catheterize
.bash_profile does not necessarily contain valid zsh code; bash and zsh are two similar, but distinct, shells.Reynaldoreynard
In my case .bash_profile was oh-my-zsh. If anybody struggles with that.Ludhiana
G
27

For Mac users:

Alongside the following: nvm, iterm2, zsh

I found using the .bashrc rather than .profile or .bash_profile caused far less issues.

Simply by adding the latter to my .zshrc file:

source $HOME/.bashrc
Gilud answered 29/11, 2018 at 16:31 Comment(2)
I had to use source $HOME/.bash_profile but this is by far the best answer. I actually already had sdkman and node configured normally for bash. I added the node modules to the path but kept getting 'permission denied'. This fixes that issue! Back to coding!Galbanum
If you are already configured bash for best use, then this is the best answerBronk
A
23

Go to your terminal and run: nano .zshrc

NOTE: If you don't have this file, you may create it using the 'touch' command: touch ~/.zshrc

Paste the following anywhere below the line containing "export ZSH":

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

Now simply hit ^X and you will be asked if you would like to save. Say 'yes' and hit return, then hit return again if it says you are changing a file. You may need to restart your terminal after saving.

Try using node -v as a quick test if you've installed node before.

Acadia answered 21/9, 2021 at 22:59 Comment(4)
.zshrc is not a directory but a file, so if doesn't exists use touch .zshrc insteadMedlar
Thank you, I will update change!Acadia
This answer should be marked as the correct answer, you saved my dayPeggie
Thanks! I'm glad I was able to help :) @TomVosAcadia
S
18

On Ubuntu, after installing ZSH, and prevously on the bash terminal installed Node or other packages,

First open:

nano .zshrc

And uncomment the second line:

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

This works for me, and without writting any line, and I think this option is available on Mac too.

Simons answered 26/10, 2018 at 18:58 Comment(2)
But this doesn't work for me. And even if I do add source $HOME/bin in my .zshrc it doesn't work. I get some output on the shell prompt every time I open it.Eleonoreleonora
does not work on Mac OSHardhearted
U
14

For me the accepted answer for adding export PATH=/usr/local/share/npm/bin:$PATH to .zshrc didn't work. I tried adding the NVM_DIR as well which solved my issue.

  1. Try vi .bashrc
  2. You will find a line like the following. Copy it.

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

  3. Paste the copied content to .zshrc file

  4. Restart the terminal

I hope this solves your issue.

Unfleshly answered 21/1, 2020 at 3:22 Comment(1)
My situation: 1. I had installed Node via nvm, and everything was working. 2. Then in the meanwhile I switched from bash to zsh and node. stopped working. So the above answer was the fix for me.Kaja
G
12

Another thing to try and the answer for me was to uncomment the first export in ~/.zshrc:

# If you come from bash you might have to change your $PATH.
export PATH=$HOME/bin:/usr/local/bin:$PATH
Geosyncline answered 31/8, 2018 at 14:50 Comment(0)
S
11

for macOS users: consider using .profile instead of .bash_profile. You may still need to manually add it to ~/.zshrc:

source $HOME/.profile

Note that there is no such file by default! Quoting slhck https://superuser.com/a/473103:

Anyway, you can simply create the file if it doesn't exist and open it in a text editor.

touch ~/.profile
open -e !$

The added value is that it feels good man to use a single file to set up the environment, regardless of the shell used. Loading a bash config file in zsh felt awkward.

Quoting an accepted answer by Cos https://mcmap.net/q/13196/-what-39-s-the-difference-between-bashrc-bash_profile-and-environment

.profile is simply the login script filename originally used by /bin/sh. bash, being generally backwards-compatible with /bin/sh, will read .profile if one exists

Following Filip Ekberg's research / opinion https://mcmap.net/q/13196/-what-39-s-the-difference-between-bashrc-bash_profile-and-environment

.profile is the equivalent of .bash_profile for the root. I think the name is changed to let other shells (csh, sh, tcsh) use it as well. (you don't need one as a user)

getting back to slhck, a note of attention regarding bash:

(…) once you create a file called ~/.bash_profile, your ~/.profile will not be read anymore.

Seato answered 19/8, 2017 at 19:33 Comment(3)
Just to add my 10 cents here, don't forget to do this AND close the terminal completely. Then open it again and it should work. None of the solutions worked for me before quitting everything and reopen again. Perfect!Illconsidered
The best answer for me, as a Ubuntu user. I just needed to source the .profile file into my .zshrc file. Just add source ~/.profile at the start of the .zshrc file & reload & you should be ok.Unreality
this one did the trick for my due to using volta to install npmVive
F
11

I solved this by brew upgrade node

Florenceflorencia answered 16/6, 2020 at 15:34 Comment(1)
This worked for me just now and should be anyone's first step before attempting to hard-modify system files as a follow-up step should this not work for you.Azov
N
8

For anyone who is still having problem. Don't forget to logout and login again.

Nonscheduled answered 25/3, 2020 at 23:1 Comment(0)
A
7

In my humble opinion, first, you have to make sure you have any kind of Node version installed. For that type:

nvm ls

And if you don't get any versions it means I was right :) Then you have to type:

nvm install <node_version**>

** the actual version you can find in Node website

Then you will have Node and you will be able to use npm commands

Acetabulum answered 16/2, 2019 at 13:53 Comment(2)
Thanks @daxtersky. That's exactly what I needed.Mcburney
This did it for me too. node --version was different that nvm ls after running the command it is good now.Watkins
N
5

In my case nvm seemed to have forgotten the global default - maybe some install i did since first adding it.

Simply re-aliasing did the trick:

nvm alias default [insert version number here]

Open a new terminal in order for this to take effect

Nl answered 4/5, 2022 at 11:33 Comment(0)
N
4

In my case, reinstalling node solved the issue. Anyone can install node via below website:

https://nodejs.org/en/download/

Nerveless answered 18/11, 2020 at 3:40 Comment(1)
Reinstalling node js solves the problem because the nodejs' and npm's binary path are added to the PATH system variable as part of the setup. Reinstalling should be a last resort, setting npm path in zshrc or other solutions involving setting the path should be considered first.Castorina
T
4

It works for me in zsh. Try nvm install --latest-npm

Tambourine answered 8/10, 2021 at 7:30 Comment(1)
Why and how does it work? Is it a magic spell?Balough
D
4

Sometimes simple things work. Uninstalling node and installing again worked for me.

brew uninstall node
brew install node
npm update
Discontent answered 7/2, 2022 at 13:15 Comment(0)
D
3

I solved this by adding

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

    export PATH="$HOME/.local/bin/:$PATH"

in to .zshrc

Dissolute answered 21/2, 2023 at 3:59 Comment(0)
A
3

no need to install node again if u have installed it via nvm, just paste following in your .zshrc file.

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion"  
 # This   loads nvm bash_completion
Aerosol answered 2/6, 2023 at 8:27 Comment(0)
F
2

In my case, i installed node with NVM and after installing z Shell, node and nvm command didn't worked. So what worked for me was installing nvm again with this command :

wget https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
sudo zsh install.sh

Above commands installed nvm again, since node was already installed, it added the node path automatically in .zshrc file and everything worked.

France answered 17/9, 2019 at 7:18 Comment(0)
S
2

Using Node v12+ on Mac Catalina, global dependencies are showing up in /$HOME/.npm-global/bin

Adding

export PATH="$HOME/.npm-global/bin:$PATH"

To ~/.bash_profile to keep everything in one place, and then adding

source $HOME/.bash_profile

To ~/.zshrc if it's not already there will do the trick. No need to logout and back in, just restart terminal.

Spanish answered 13/3, 2021 at 10:11 Comment(0)
S
2

Add this at the end of your .zshrc file:

source $HOME/.zshrc.pre-oh-my-zsh 

This instantly solved my problem.

Schulman answered 5/8, 2022 at 17:23 Comment(1)
It works, but each time I checkout into other branches I need to run it again...Kat
G
1

FOR MAC: I tried some of the above but to no avail, could not get anything to work.

I did have BREW INSTALLED, so although this not be the best approach, with zsh, I did:

  1. sudo chown -R $(whoami) /usr/local/share/man/man8 (for brew access)
  2. brew update && brew install npm (I had had node installed)
  3. npm -v (to confirm install)
  4. nano ~/.zshrc (to empty file changes and save)

This worked for me. Hope this helps someone. #1 bothers me, but I will live with for now.

Gynophore answered 12/9, 2020 at 20:25 Comment(0)
B
1

Just writing this in my terminal fixed the issue. Note that I had to write it separately in my project folder.

export PATH="$HOME/.npm-packages/bin:$PATH"
export PATH="$HOME/.node/bin:$PATH"

Source: https://cloudlinuxtech.com/npm-command-not-found-fix/

Bulimia answered 27/12, 2021 at 8:3 Comment(0)
U
1

I got this message after installing using Homebrew

enter image description here

So I added these lines to the .zshrc and it worked.

export PATH="/usr/local/opt/node@16/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/node@16/lib"
export CPPFLAGS="-I/usr/local/opt/node@16/include"
Undercarriage answered 27/9, 2022 at 10:2 Comment(0)
R
0

I think the problem is more about the ZSH completion.

You need to add this line in your .zshrc:

zstyle ':completion:*' rehash true

If you have Oh-my-zsh, a PR has been made, you can integrate it until it is pulled: https://github.com/robbyrussell/oh-my-zsh/issues/3440

Riedel answered 29/6, 2015 at 17:39 Comment(0)
W
0

I had the same problem while installing Vercel CLI.

In my case, install with sudo.

example: sudo npm i -g vercel

Watersoak answered 9/6, 2021 at 1:24 Comment(0)
I
0

For OSX Catalina users

brew install node@14

I'm using node 14 cause angular won't support latest version (Don't know why).. Then

echo 'export PATH="/usr/local/opt/node@14/bin:$PATH"' >> ~/.zshrc
Isochroous answered 9/9, 2021 at 8:22 Comment(3)
I’ve heard Brew is not ideal for production environments because it may cause issues with company security policies. I would not recommend this unless it is your personal machine.Acadia
Thanks @Acadia I'll keep this in mind, never thought that brew has some security issues. Thanks a lot.Isochroous
It’s not Brew, it’s more so that your company may not give permissions for certain actionsAcadia
C
0

In my case this happened after I configured my iterm2 to show git branch names. I basically made my iterm2 to look more elegant. But I noticed my vscode terminal was detecting nvm and npm commands

I have added some lines to .zshrc file when I am configuring iterm2. I had added oh-my-zsh.

But adding the below line to the top of .zshrc file worked for me. If it does not work kill vscode terminal and open it again

Hope this will help someone

export NVM_DIR=~/.nvm
 [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
Conscience answered 19/8, 2022 at 1:44 Comment(0)
S
0

For Windows using windows-nvm I had issues after installing an additional version of node. My npm stopped working but only in ZSH. bash, Powershell, and cmd all worked fine. What ended up fixing it was adding an additional alias to zsh of alias npm="npm.cmd" and it started working again. I think it's zsh struggling to find the correct binary, so the .cmd file fixed this issue.

Sapsucker answered 14/11, 2022 at 18:43 Comment(0)
F
0

I had node installed with nvm, and the issue i had is that i had 2 versions of node, and i used them with command nvm use v12.xx.0. But somehow they both got detached, and i had to use it again with nvm one of them, and it started to work again how it should. So the command is(if you have at least one version installed with nvm):

nvm use vxx.xx.0

Freshwater answered 9/1, 2023 at 15:57 Comment(0)
E
0

I had zsh shell and Node installed along with npmrc. I had been using it for years when suddenly it started giving me this error. None of the answers here helped me with this problem.

Turns out I had recently updated my Node version and I had to install npmrc again. So if anyone is having this problem after upgrading their node version, try

npm install -g npmrc

Endocardial answered 20/1, 2023 at 9:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.