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

👌

相关文章
相关标签/搜索