Terminal (zsh) command not found for global npm installs - macos

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

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

I have no OSX to try... but the paths for zsh might belong into there:
/etc/paths, /private/etc/paths or into /private/etc/paths.d
Beside that, there's an auto-complete plugin for npm.

Related

$PATH error every time Terminal is Opened

I had installed Postgres.app before and after I uninstalled via App Cleaner now every time I open terminal this error appears in the first line I should mention that a followed what documentation for uninstalling the Postgres on their website but it doesn't work for me.
Warning: $PATH entry "/Applications/Postgres.app/Contents/Versions/9.6/bin" is not valid (No such file or directory)
.bash_profile:
# source ~/.profile
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
export PATH="/usr/local/opt/node#6/bin:$PATH"
how can I get rid of this annoying warning?
Thanks.
You should look at all bash initialization files. There may be many, depending on your distro. There's a good list on tldp:
/etc/bash.bashrc
/etc/profile
/etc/bashrc
~/.bash_profile
~/.bash_login
~/.profile
~/.bashrc
Consider that some of these files may also include all files in some directory. For example on most instances /etc/profile will run every .sh file in /etc/profile.d/, this may include a paths file called /etc/profile.d/extrapaths.sh.
EDIT:
After checking the documentation for PostgresApp (1, 2), and this issue I found that it uses a file called /etc/paths.d/postgresapp. Be sure to check it.
If you'd like to remove the paths, it states that you should run the following command:
sudo rm /etc/paths.d/postgresapp

"-bash: grunt: command not found, successful installation"

I'm learning grunt and seem to run in a bit of problem within the terminal. When I run a grunt command, I get:
-bash: grunt: command not found, successful installation
My bash_profile reads
### Added by the Heroku Toolbelt
export PATH="/usr/local/heroku/bin:$PATH"
export PATH="/usr/local/bin:$PATH"
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
export NVM_DIR="/Users/colette/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
and .bash reads
export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting
I don't see anything with grunt and I'm wondering whats missing if anything.
Edit
I'm now getting
-bash: npm: command not found
Not sure if it's related to my previous problem. It's also weird because I used the npm to install grunt.
Update:
Hmmmm...I had to run nvm again for it to work. But I'm still having a problem with grunt
I was able to figure out the problem through this walkthrough
https://www.digitalocean.com/community/tutorials/how-to-install-node-js-with-nvm-node-version-manager-on-a-vps
I was missing the PATH for grunt in the .bash_profile.
Not sure if I should create a new post, but I now have another question.
Why do I have to run nvm every time I start terminal? Is there a way to just have it ready to go without having to nvm install [version number]?

Where did I go wrong in virtualenv installation on OSX?

I followed the instructions carefully at http://jamie.curle.io/blog/installing-pip-virtualenv-and-virtualenvwrapper-on-os-x/
Restarted my terminal and typed mkvirtualenv, got a command not found
looked in /usr/local/bin and ls shows me:
brew powerpc-apple-darwin8-gfortran-4.2
charm tclsh8.5
django-admin.py tclsh8.6
django-admin.pyc virtualenv
gfortran-4.2 virtualenv-2.7
gfortran-uninstall virtualenv-clone
i686-apple-darwin8-gfortran-4.2 virtualenvwrapper.sh
node virtualenvwrapper_lazy.sh
pip wish8.5
pip2 wish8.6
So I know it's in the correct directory. I don't understand where I could have screwed up.
The linked article recommends adding the line source /usr/local/bin/virtualenvwrapper.sh to bash login profile file ~/.bash_login.
However,
~/.bash_login is rarely used.
it is NOT read if the much more common (on OS X) ~/.bash_profile also exists.
Thus,
Check if you have a ~/.bash_profile file
and, if so, add the line source /usr/local/bin/virtualenvwrapper.sh there.
My recommendation is to never use ~/.bash_login. If you find other content, there, also add it to ~/.bash_profile, and delete ~/.bash_login.
Alternatively, if you're looking for cross-platform compatibility, put all definitions in ~/.bashrc instead, and add only [[ -e ~/.bashrc ]] && . ~/.bashrc to ~/.bash_profile.

How to change path for homebrew: "config" scripts exist outside your system or Homebrew directories

Goal: install Ruby 2.0.0
Brew Doctor says:
Warning: "config" scripts exist outside your system or Homebrew directories. `./configure`
scripts often look for *-config scripts to determine if
software packages are installed, and what additional flags to use when
compiling and linking.
Having additional scripts in your path can confuse software installed via
Homebrew if the config script overrides a system or Homebrew provided
script of the same name. We found the following "config" scripts:
/opt/sm/pkg/active/bin/curl-config
/opt/sm/pkg/active/bin/ncurses5-config
/opt/sm/pkg/active/bin/ncursesw5-config
/opt/sm/pkg/active/bin/pkg-config
/opt/sm/pkg/active/bin/xml2-config
/opt/sm/pkg/active/bin/xslt-config
I found the answer here: Brew doctor gives out warnings but don't know how because he didn't tell us how to do it
I need to remove /opt/sm/pkg/active/bin/ from my path.
$ echo $PATH
/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/sm/bin:/opt/sm/pkg/active/bin:/opt/sm/pkg/active/sbin:/Development/android-sdk-macosx/platform-tools:/Development/android-sdk-macosx/tools
but when I open my file
touch ~/.bash_profile; open ~/.bash_profile
I get:
export PATH=${PATH}:/Development/android-sdk-macosx/platform-tools:/Development/android-sdk-macosx/tools
##
# Your previous /Users/john/.bash_profile file was backed up as /Users/john/.bash_profile.macports-saved_2013-06-06_at_14:30:25
##
# MacPorts Installer addition on 2013-06-06_at_14:30:25: adding an appropriate PATH variable for use with MacPorts.
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
# Finished adapting your PATH environment variable for use with MacPorts.
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
Notice the path I'm trying to remove is not in the file, but it's in my path. What do I do??
The /opt/sm is in the /etc/profile.d directory. Open it by entering
cd /etc/profile.d
in terminal and then
open .
Open sm.sh file in text editor and then
change
PATH="${PATH}:/opt/sm/bin:/opt/sm/pkg/active/bin:/opt/sm/pkg/active/sbin"
to
#PATH="${PATH}:/opt/sm/bin:/opt/sm/pkg/active/bin:/opt/sm/pkg/active/sbin"
and then quit and relaunch Terminal. Run brew doctor 'Warning' should be gone.
It seems that "sm" comes from RailsInstaller, at least in my case. Following the approach in this post, I commented out the line in /etc/profile.d/sm.sh and restarted Terminal. This solved the above brew doctor errors.

Command not found after npm install in zsh

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
vows#0.6.4 /usr/local/share/npm/lib/node_modules/vows
├── eyes#0.1.8
└── diff#1.0.3
[❤ ~/Desktop/sauce-node-demo:master] vows
zsh: command not found: vows
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
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.
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
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.
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
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.
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.
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.
Try vi .bashrc
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
Paste the copied content to .zshrc file
Restart the terminal
I hope this solves your issue.
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://stackoverflow.com/a/415444/2445063
.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://stackoverflow.com/a/415410/2445063
.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.
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
I solved this by brew upgrade node
For anyone who is still having problem. Don't forget to logout and login again.
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
In my case, reinstalling node solved the issue. Anyone can install node via below website:
https://nodejs.org/en/download/
It works for me in zsh. Try nvm install --latest-npm
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
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.
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.
Sometimes simple things work. Uninstalling node and installing again worked for me.
brew uninstall node
brew install node
npm update
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:
sudo chown -R $(whoami) /usr/local/share/man/man8 (for brew access)
brew update && brew install npm (I had had node installed)
npm -v (to confirm install)
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.
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/
I got this message after installing using Homebrew
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"
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
I had the same problem while installing Vercel CLI.
In my case, install with sudo.
example: sudo npm i -g vercel
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
Add this at the end of your .zshrc file:
source $HOME/.zshrc.pre-oh-my-zsh
This instantly solved my problem.
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"
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.
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
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

Resources