找不到全局 npm 安装的终端 (zsh) 命令

Terminal (zsh) command not found for global npm installs

这让我抓狂!我做了很多谷歌搜索并尝试了各种方法。 (我不认为这是超级用户主题)

我最近在使用终端时遇到了很多麻烦。我一定是在某个地方搞砸了,因为它曾经工作得很好,现在我无法让它识别我的命令,无论是 nvm 还是 global npm 包,比如 expo。它只是给我这样的错误:

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

(顺便说一句:找到 npmbrewj 命令)

如果我这样做 echo $PATH 我得到:

/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

(奇怪的行为是,如果我再次执行 echo $PATH 它会 returns 在一行中输出相同的两倍)

它的可读版本(将 : 替换为 :\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

我的 .zshrc 文件如下所示:

# 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

如有任何帮助,我们将不胜感激!

更新

阅读@l'L'l 和 this answer 的友善回答,我做了以下事情:

将我的 .bash_profile 更新为

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

(这是该文件中唯一的东西)

$touch .bashrc 创建了一个 .bashrc(可能删除了旧的..但这只是一个空文件..)

将以下行添加到 .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

做了 source ~/.zshrc 并重新启动了我的终端。

现在 nvmexpo 有效! 谢谢

我没有 OSX 可以尝试...但是 zsh 的路径可能属于那里:

/etc/paths/private/etc/paths/private/etc/paths.d

除此之外,还有一个 auto-complete plugin 代表 npm

您可以尝试一些方法,第一个是从您的 .zshrc 文件中获取 ~/.bash_profilenvm 命令可能是在那里设置的,而您的 zsh shell 根本不知道它存在。

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

↑ 由于您在 .zshrc.

中的 source ~/.bash_profile & ~/.bashrc 处使用了 zsh

如果您使用 homebrew 安装,那么您可能需要将以下内容添加到 .zshrc 中:

export/source nvm 安装 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 未通过 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