npm全局安裝權限不足

本文出處https://shenyifengtk.github.io/
如有轉載,請說明出處

之前在自己學習ubuntu電腦上搭建一個hexo博客時,發現 npm install -g hexo-cli居然出現

ting@whtll:~$ npm install -g hexo-cli
npm WARN checkPermissions Missing write access to /opt/node-v10.16.0-linux-x64/lib/node_modules
npm ERR! path /opt/node-v10.16.0-linux-x64/lib/node_modules
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall access
npm ERR! Error: EACCES: permission denied, access '/opt/node-v10.16.0-linux-x64/lib/node_modules'
npm ERR!  { [Error: EACCES: permission denied, access '/opt/node-v10.16.0-linux-x64/lib/node_modules']
npm ERR!   stack:
npm ERR!    'Error: EACCES: permission denied, access \'/opt/node-v10.16.0-linux-x64/lib/node_modules\'',
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/opt/node-v10.16.0-linux-x64/lib/node_modules' }
npm ERR!
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR!
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator (though this is not recommended).

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/ting/.npm/_logs/2019-07-17T08_30_56_668Z-debug.log

其實原因很簡單的,npm會把二進制執行代碼安裝到${Node}/node_modules/,但是這麼目類root擁有的,普通用戶沒有權限寫的。我在網上查了下資料,大概有三種解決方法。

修改權限

直接將node_modules目類改成777,這個太暴力,也不安全,pass。

將目類擁有者改成當前普通用戶,這個當時我自己當時想出來的辦法,居然也是失敗了😟。

重裝NodeJs

很多網友推薦使用mvn教程重裝Nodejs,直接執行xshell腳本安裝mvn命令。

curl -o- https://raw.githubusercontent... | bash

重裝Nodejs

nvm install node

其實這種方法也不是很完美的,如果做個Java都知道,這個maven的命令,但是這個mvn又不是maven來的,命令衝突了,pass。

正確處理方式


官方
發現有一個不錯的處理方式,直接搬過來。

創建目錄,用於存放npm 全局安裝二進制執行文件

mkdir ~/.npm-global

配置npm以使用新的目錄路徑


npm config set prefix '~/.npm-global'

使用編輯器打開.bashrc文件設置環境變量,這個文件環境變量知道當前用戶生效,添加下面這句話到文件結尾,保存退出。

export PATH=~/.npm-global/bin:$PATH

更新環境變量

source .bashrc

👌

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章