zsh上找不到npm全局安装命令

4

我刚刚使用npm install -g live server安装了live-server包。

我检查过,该包当前已安装在以下路径:

/Users/username/.npm-global/lib/node_modules/live-server/live-server.js

但是,当我尝试从命令行运行live-server时,出现了错误:

zsh: command not found: live-server

我需要做什么才能运行live-server呢?我已经安装了oh-my-zsh,我担心这可能是导致问题的原因。我知道这应该与设置我的PATH有关,但我不确定具体是什么。

运行echo $PATH的结果如下:

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/opt/ImageMagick/bin:/Users/username/npm-global/bin:/Users/username/npm-global/bin:/Users/username/npm-global/lib

这里有一些可能相关的根目录中的文件。

谢谢。

.zshrc

# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH

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

# Set name of the theme to load. Optionally, if you set this to "random"
# it'll load a random theme each time that oh-my-zsh is loaded.
# See https://github.com/robbyrussell/oh-my-zsh/wiki/Themes
ZSH_THEME="robbyrussell"

# Set list of themes to load
# Setting this variable when ZSH_THEME=random
# cause zsh load theme from this variable instead of
# looking in ~/.oh-my-zsh/themes/
# An empty array have no effect
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )

# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"

# Uncomment the following line to use hyphen-insensitive completion. Case
# sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"

# Uncomment the following line to disable bi-weekly auto-update checks.
# DISABLE_AUTO_UPDATE="true"

# Uncomment the following line to change how often to auto-update (in days).
# export UPDATE_ZSH_DAYS=13

# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"

# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"

# Uncomment the following line to enable command auto-correction.
# ENABLE_CORRECTION="true"

# Uncomment the following line to display red dots whilst waiting for completion.
# COMPLETION_WAITING_DOTS="true"

# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"

# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# The optional three formats: "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# HIST_STAMPS="mm/dd/yyyy"

# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder

# Which plugins would you like to load? (plugins can be found in ~/.oh-my-zsh/plugins/*)
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(
  git
)

source $ZSH/oh-my-zsh.sh

# User configuration

# export MANPATH="/usr/local/man:$MANPATH"

# You may need to manually set your language environment
# export LANG=en_US.UTF-8

# Preferred editor for local and remote sessions
# if [[ -n $SSH_CONNECTION ]]; then
#   export EDITOR='vim'
# else
#   export EDITOR='mvim'
# fi

# Compilation flags
# export ARCHFLAGS="-arch x86_64"

# ssh
# export SSH_KEY_PATH="~/.ssh/rsa_id"

# Set personal aliases, overriding those provided by oh-my-zsh libs,
# plugins, and themes. Aliases can be placed here, though oh-my-zsh
# users are encouraged to define aliases within the ZSH_CUSTOM folder.
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"
export PATH=$PATH:/Users/username/npm-global/bin
export PATH=$PATH:/Users/username/npm-global/lib

1
为什么你的文件 .bashrc.bash_profile 是相关的?如果你运行 zsh,当你加载一个交互式 shell 时,这些文件都不会被调用。只使用 .zshrc.zprofile 来配置你的 zsh shell,就像 这个跨站点重复问题 中所示。 - Aserre
谢谢提醒,我会删除那些文件。 - Jon_B
你的 /Users/username/npm-global/bin 文件夹中有什么内容?另外,当你运行 bash 而不是 zsh 时,它是否有效? - Aserre
顺便提一下,您的 shell 仅会读取 PATH 文件夹直接位于根目录下的文件。正如您在帖子中展示的那样,live-server.js 似乎位于子文件夹中,因此 zsh 不会知道它的存在。 - Aserre
另外,在/Users/username/npm-global/bin中,您在提问时是否打错了字?应该是/Users/username/.npm-global/bin(有一个点)。 - Aserre
非常感谢。将export PATH=$PATH:/Users/username/.npm-global/bin添加到.zshrc而不是.bashrc中已经解决了我的问题。 - Jon_B
4个回答

8
export PATH=$PATH:/Users/username/.npm-global/bin添加到.zshrc而不是.bashrc中已解决我的问题。

5
将此添加到我的~/.zshrc文件中,问题得到了解决。
export PATH="$HOME/.npm-packages/bin:$PATH"

1
但是那个名为 .npm-packages 的文件夹不存在。 - Matteo

0

按照以下方式设置以防止进一步问题:

安装NVM来管理Node版本 使用--lts选项安装最新版本的node 再次安装最新版本的NPM

之后正确设置.zshrc文件。如果您使用zshell,请勿使用.bashrc,它有自己的配置文件,我认为您已经更新了它。

检查NPM页面https://www.npmjs.com/package/live-server,特别是这些部分:

Default options:
If a file ~/.live-server.json exists it will be loaded and used as default options for live-server on the command line. See "Usage from node" for option names.

Usage from node
var liveServer = require("live-server");
var params = ...

0

我忘记了我在使用nvm(Node版本管理器),但是我通过输入以下内容发现它:

where npm

这为我提供了当前npm安装路径。

/Users/myusername/.nvm/versions/node/v16.13.0/bin/npm

这意味着我的全局安装的包在 /Users/myusername/.nvm/versions/node/v16.13.0/lib/node_modules 中!

因此,我在我的 zshconfig 中执行了以下操作:

export PATH="$PATH":"$HOME/.nvm/versions/node/v16.13.0/lib/node_modules"

终端重启后,我的全局安装的包开始工作了!

注意:如果您更改了活动的节点版本(通过nvm use ...),它可能会破坏您全局安装的软件包。但是在更改节点版本后,您始终可以更新路径(如果这是一个长期更改)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接