爆款云服务器低至4折,1核1G¥366/

普通使用使用 Node.js npm -g 全局安装,出现权限错误 checkPermissions Missing write access permission denied

[tumobi@localhost ~]$ npm install -g pm2
npm WARN checkPermissions Missing write access to /usr/local/nodejs/lib/node_modules
npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /usr/local/nodejs/lib/node_modules
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, access '/usr/local/nodejs/lib/node_modules'
npm ERR!  { [Error: EACCES: permission denied, access '/usr/local/nodejs/lib/node_modules']
npm ERR!   stack:
npm ERR!    'Error: EACCES: permission denied, access \'/usr/local/nodejs/lib/node_modules\'',
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/local/nodejs/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.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/tumobi/.npm/_logs/2020-01-04T10_43_44_660Z-debug.log

根据提示 the command again as root/Administrator. 使用 root 用户或是使用 sudo 重新执行

sudo npm -g install pm2

但是又出错了 sudo: npm:找不到命令 , npm -v 和 node -v 都正常,但是添加 sudo 合都提示找不命令。

切换回 root 用户再操作,则问题解决

su -
npm install -g pm2
pm2 -v

再次切换回普通用户 tumobi 查看 pm2 是否安装

pm2 -v

到此一切正常!

sudo npm -g install pm2 sudo: npm:找不到命令 解决方法

附另一种解决方法:https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally#reinstall-npm-with-a-node-version-manager

标签: Node.js, CentOS