npm全局安装后“command not found”的解决办法

11,887 阅读1分钟
一般来说是PATH配置的问题。可以使用命令

npm config ls

来查看全局安装到的目录。如返回结果

node bin location = /usr/local/Cellar/node/11.12.0/bin/node

说明全局目录为: /usr/local/Cellar/node/11.12.0/bin/

然后再系统的全局环境配置文件中,添加以下内容(我用的是zsh,vim ~/.zshrc):

export PATH="/usr/local/Cellar/node/11.12.0/bin/:$PATH"   // 这里填入上一步返回的全局路径

执行命令,让配置文件生效

source ~/.zshrc 

然后重启命令行!

如果遇到权限问题,执行下

sudo chmod a+rwx /usr/local/Cellar/node/11.12.0/lib/node_modules/  
和
sudo chmod a+rwx /usr/local/Cellar/node/11.12.0/bin/

完~