npm全局安裝權限不足

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

以前在本身學習ubuntu電腦上搭建一個hexo博客時,發現 npm install -g hexo-cli竟然出現node

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擁有的,普通用戶沒有權限寫的。我在網上查了下資料,大概有三種解決方法。linux

修改權限

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

將目類擁有者改爲當前普通用戶,這個當時我本身當時想出來的辦法,竟然也是失敗了😟。github

重裝NodeJs

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

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

重裝Nodejsnpm

nvm install node

其實這種方法也不是很完美的,若是作個Java都知道,這個maven的命令,可是這個mvn又不是maven來的,命令衝突了,pass。ubuntu

正確處理方式


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

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

mkdir ~/.npm-global

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

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

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

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

更新環境變量

source .bashrc

👌

相關文章
相關標籤/搜索