Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terminal (zsh) command not found for global npm installs

This is driving me nuts! I did a lot of googling and tried various things. (I do not consider this to be a superuser topic)

I'm having a lot of troubles with terminal lately. I must have messed up somewhere, because it used to work just fine and now I can't get it to recognize my commands anymore neither nvm or global npm packages like expo. It just gives me errors like this:

▶ expo      
zsh: command not found: expo
▶ nvm ls
zsh: command not found: nvm

(BTW: npm, brew and j commands are found 🤔)

If I do echo $PATH I get:

/Users/norfeldt/Library/Android/sdk/tools/bin:/Users/norfeldt/Library/Android/sdk/tools:/Users/norfeldt/Library/Android/sdk/platform-tools:/Applications/anaconda/bin:~/Library/Python/2.7/bin:~/.npm-global/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

(strange behavior is that if I do echo $PATH again it returns two times the same output in one line)

A readable version of it (replacing : with :\n

/Users/norfeldt/Library/Android/sdk/tools/bin:
/Users/norfeldt/Library/Android/sdk/tools:
/Users/norfeldt/Library/Android/sdk/platform-tools:
/Applications/anaconda/bin:
~/Library/Python/2.7/bin:
~/.npm-global/bin:
/usr/local/bin:
/usr/bin:
/bin:
/usr/sbin:
/sbin

My .zshrc file looks like this:

# Node & NPM
#PATH="/usr/local/bin:$PATH"
PATH="~/.npm-global/bin:$PATH"
#PATH="~/.npm-global/lib/node_modules:$PATH"

# Git
alias master="git checkout master"
alias dev="git checkout develop"
alias hotfix="git flow hotfix"
alias feature="git flow feature"
alias tags="git push --tags"

# Pip - https://gist.github.com/haircut/14705555d58432a5f01f9188006a04ed
PATH="~/Library/Python/2.7/bin:$PATH"

# added by Anaconda2 4.4.0 installer
PATH="/Applications/anaconda/bin:$PATH"

# Android
export ANDROID_HOME=/Users/norfeldt/Library/Android/sdk
PATH="${ANDROID_HOME}/platform-tools:$PATH"
PATH="${ANDROID_HOME}/tools:$PATH"
PATH="${ANDROID_HOME}/tools/bin:$PATH"

alias emu="pushd ${ANDROID_HOME}/tools;emulator -avd Pixel_2; popd"

# Path to your oh-my-zsh installation.
export ZSH=/Users/norfeldt/.oh-my-zsh

ZSH_THEME="avit"

# Autojump
[[ -s `brew --prefix`/etc/autojump.sh ]] && . `brew --prefix`/etc/autojump.sh

# shell startup.
plugins=(git)

source $ZSH/oh-my-zsh.sh

# Load zsh-autosuggestions.
source /usr/local/share/zsh-autosuggestions/zsh-autosuggestions.zsh

# zsh-syntax-highlighting
source /Users/norfeldt/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

export PATH

ANY help would be HIGHLY appreciated!

UPDATE

Reading the kind answer from @l'L'l and this answer I did the following:

Updated my .bash_profile to

export NVM_DIR=~/.nvm
source ~/.nvm/nvm.sh

(it's the only thing in that file)

created a .bashrc by $touch .bashrc (Might have deleted the old one.. But this is just an empty file..)

Added the following lines to .zshrc

PATH="$PATH:$HOME/.npm-global/bin/"             # Changed ~ to $HOME
PATH="$PATH:$HOME/.npm-global/lib/node_modules" # Changed ~ to $HOME
...
# Bash stuff
source ~/.bashrc
source ~/.bash_profile

Did a source ~/.zshrc and restarted my terminal.

NOW the nvm AND expo works! THANKS

like image 547
Norfeldt Avatar asked Jul 08 '19 08:07

Norfeldt


People also ask

How do I fix zsh command not found npm Mac?

Fix npm command not found error on macOS or Linux First, you need to uninstall npm using the same method you installed it. If you use a package manager like Homebrew, then use brew uninstall node to remove it. Then you can install your NodeJS and npm using brew install node command.

What is the command to install npm globally?

NPM installs global packages into /<User>/local/lib/node_modules folder. Apply -g in the install command to install package globally.

Where do I install global npm?

Path of Global Packages in the system: Global modules are installed in the standard system in root location in system directory /usr/local/lib/node_modules project directory. Command to print the location on your system where all the global modules are installed.


1 Answers

There are a few things you might try, the first would be to source ~/.bash_profile from your .zshrc file. It's possible the nvm command was setup there and your zsh shell simply doesn't know it exists.

Note: On OS X, if you get nvm: command not found after running the install script, one of the following might be the reason:-

your system may not have a .bash_profile file where the command is set up. Simply create one with touch ~/.bash_profile and run the install script again you might need to restart your terminal instance. Try opening a new tab/window in your terminal and retry. If the above doesn't fix the problem, open your .bash_profile and add
the following line of code:

source ~/.bashrc

For more information about this issue and possible workarounds, please refer here

↑ Since you are using zsh instead at source ~/.bash_profile & ~/.bashrc in .zshrc.


If you used homebrew to install, then you might want to add the following into .zshrc:

export/source nvm installed with homebrew:

# source nvm
export NVM_DIR=~/.nvm

if hash brew 2>/dev/null; then
    source $(brew --prefix nvm)/nvm.sh
    source `brew --prefix`/etc/profile.d/z.sh
fi

npm not installed via homebrew:

export NVM_DIR="~/.nvm"
source ~/.nvm/nvm.sh
[[ -s "$NVM_DIR/nvm.sh" ]] && \. "$NVM_DIR/nvm.sh" # load nvm               
[[ -s "$NVM_DIR/bash_completion" ]] && \. "$NVM_DIR/bash_completion" # load nvm bash_completion

↳ https://github.com/nvm-sh/nvm

like image 134
l'L'l Avatar answered Oct 07 '22 13:10

l'L'l