MacOS 終端執行http-server命令,開啓本地服務器時,遇到以下報錯信息
coolpeng:~ littlelittlepeng$ http-server
-bash: http-server: command not found
報錯原因
npm全局安裝路徑沒有被加入到bash的$PATH環境變量中
解決辦法
- 執行以下命令,查看http-server的安裝路徑,步驟3中需要用到
npm install http-server -g
- 終端輸入以下命令,打開bash環境編輯頁面
open .bash_profile
- 加入以下環境變量,注意bin: 後邊的路徑爲步驟1中獲取到的安裝路徑
export PATH=$PATH:$M2_HOME/bin:/usr/local/Cellar/node/9.4.0/lib/node_modules/http-server/bin/
- 保存bash_profile文件,然後在終端中輸入source .bash_profile命令,即可生效。
- 終端執行以下命令,啓動本地服務器成功
http-server