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

👌

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