npm 安裝 @vue/cli 報錯問題解決

使用的是 macOS 系統,在終端中使用命令安裝。

安裝命令

sudo npm install -g @vue/cli

結果報錯

gyp WARN EACCES current user ("nobody") does not have permission to access the dev dir "/Users/wanchao/Library/Caches/node-gyp/14.15.3"
gyp WARN EACCES attempting to reinstall using temporary dev dir "/usr/local/node/lib/node_modules/@vue/cli/node_modules/fsevents/.node-gyp"
gyp WARN install got an error, rolling back install
gyp WARN install got an error, rolling back install
gyp ERR! configure error 
gyp ERR! stack Error: EACCES: permission denied, mkdir '/usr/local/node/lib/node_modules/@vue/cli/node_modules/fsevents/.node-gyp'
gyp ERR! System Darwin 20.2.0
gyp ERR! command "/usr/local/node/bin/node" "/usr/local/node/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /usr/local/node/lib/node_modules/@vue/cli/node_modules/fsevents
gyp ERR! node -v v14.15.3
gyp ERR! node-gyp -v v5.1.0
gyp ERR! not ok 

由報錯信息中permission denied可以看出存在權限問題。

解決方法

sudo npm install -g @vue/cli --unsafe-perm
  • npm 出於安全考慮不支持以 root 用戶運行,即使你用 root 用戶身份運行了,npm 會自動轉成一個叫nobody的用戶來運行,而這個用戶幾乎沒有任何權限。
  • 這樣的話如果你腳本里有一些需要權限的操作,比如寫文件(尤其是寫 .node-gyp),就會崩掉。
  • 爲了避免這種情況,要麼按照 npm 的規矩來,專門建一個用於運行 npm 的高權限用戶;要麼加--unsafe-perm參數,這樣就不會切換到nobody上,運行時是哪個用戶就是哪個用戶,即使是 root。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章